uniapp打开地图直接获取位置
创始人
2024-11-04 19:36:30
0

在这里插入图片描述
uniapp官网文档

https://en.uniapp.dcloud.io/api/location/open-location.html

	 		 			{{item.distance||'0'}}km 		 	 
	import map from '../../utils/map.js' 		onLoad() { 			let that = this 			let addressInfo = getApp().globalData.addressInfo; 			if (addressInfo) { 				that.addressInfo = addressInfo 				that.getOilList() 			} else { 			//这里是获取地理位置 				map.loadCity().then(res => { 					that.addressInfo = getApp().globalData.addressInfo 					that.getOilList() 				}); 			} 		}, // 点击获取地图 	kilometer(e) { 		uni.openLocation({ 			longitude: Number(e.lng), 			latitude: Number(e.lat), 			name: e.name, 			address: e.address 		}) 	}, 

map.js页面对地理位置进行封装

 import QQMapWX from '@/utils/qqmap-wx-jssdk.min.js' var qqmapsdk = {};   // 获取位置授权 async function loadCity() {     let that = this;     return new Promise(function (resolve, reject) {         uni.getSetting({             success: (res) => {                 // res.authSetting['scope.userLocation'] == undefined    表示 初始化进入该页面                 // res.authSetting['scope.userLocation'] == false    表示 非初始化进入该页面,且未授权                 // res.authSetting['scope.userLocation'] == true    表示 地理位置授权                 if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {                     uni.showModal({                         title: '请求授权当前位置',                         content: '需要获取您的地理位置,请确认授权',                         success: function (res) { 							                             if (res.cancel) {                                 uni.showToast({                                     title: '拒绝授权',                                     icon: 'none',                                     duration: 1000                                 })                                 reject(false);                             } else if (res.confirm) {                                 uni.openSetting({                                     success: function (dataAu) {                                         if (dataAu.authSetting["scope.userLocation"] == true) {                                             uni.showToast({                                                 title: '授权成功',                                                 icon: 'success',                                                 duration: 1000                                             })                                             that.getLocation().then(function (res) {                                                 if (res) {                                                     resolve(true);                                                 } else {                                                     reject(false);                                                 }                                              });                                         } else {                                             uni.showToast({                                                 title: '授权失败',                                                 icon: 'none',                                                 duration: 1000                                             })                                             reject(false);                                         }                                     }                                 })                             }                         }                     })                 } else if (res.authSetting['scope.userLocation'] == undefined) {                     that.getLocation().then(function (res) {                         if (res) {                             resolve(true);                         } else {                             reject(false);                         }                      });                 } else {                     that.getLocation().then(function (res) {                         if (res) {                             resolve(true);                         } else {                             reject(false);                         }                      });                  }             }         })     }).catch((e) => {}) } //坐标获取城市 function getLocation() {     let vm = this;     return new Promise(function (resolve, reject) {         uni.getLocation({             type: 'wgs84',             success: function (res) {                 getApp().globalData.latitude = res.latitude;                 getApp().globalData.longitude = res.longitude;                 uni.setStorageSync("longitude", res.longitude)                 uni.setStorageSync("latitude", res.latitude)                 vm.getLocal().then(function (res) {                     if (res) {                         resolve(true);                     } else {                         reject(false);                     }                 });             },             fail: function (res) {                 reject(false);             }         })     }).catch((e) => {}) }  // 坐标转换地址 function getLocal() {     let vm = this;     return new Promise(function (resolve, reject) {         qqmapsdk = new QQMapWX ({             key: 'asdfghjklqwertyuiop' //这里自己的key秘钥进行填充         });         qqmapsdk.reverseGeocoder({             location: {                 latitude: getApp().globalData.latitude,                 longitude: getApp().globalData.longitude             },             success: function (res) {                 getApp().globalData.addressInfo = res.result.address_component;                 resolve(true);             },             fail: function (res) {                 reject(false);             }         });     }).catch((e) => {}) }  function calculateDistance(latitude, longitude) {     let vm = this;     return new Promise(function (resolve, reject) {         qqmapsdk = new QQMapWX ({             key: 'asdfghjklqwertyuiop' //这里自己的key秘钥进行填充         });         qqmapsdk.calculateDistance({             to: [{                 latitude: latitude, //商家的纬度                 longitude: longitude, //商家的经度             }],             success: function (res) {                 resolve(res);             },             fail: function (res) {                 reject(res);             }         });     }).catch((e) => {}) }  function selectLocation() {     let that = this;     return new Promise(function (resolve, reject) {         uni.getSetting({             success(res) {                 // 只返回用户请求过的授权                 let auth = res.authSetting;                 if (auth['scope.userLocation']) {                     // 已授权,申请定位地址                     resolve(true)                 } else if (auth['scope.userLocation'] === undefined) {                     // 用户没有请求过的授权,不需要我们主动弹窗,微信会提供弹窗                     resolve(true)                 } else if (!auth['scope.userLocation']) {                     // 没有授权过,需要用户重新授权                     // 这个弹窗是为了实现点击,不然openSetting会失败                     uni.showModal({                         title: '是否授权当前位置?',                         content: '需要获取您的地理位置,请确认授权,否则定位功能将无法使用',                         success: res => {                             if (res.confirm) {                                 uni.openSetting({                                     success(res) {                                         let setting = res.authSetting;                                         if (!setting['scope.userLocation']) {                                             uni.showToast({                                                 title: '地址授权失败,定位功能无法使用',                                                 icon: 'none',                                             });                                             reject(false)                                         } else {                                             // 地址授权成功,申请定位地址                                             resolve(true)                                         }                                     },                                     fail(err) {                                         // 需要点击,有时候没有点击,是无法触发openSetting                                         console.log('open-setting-fail', err);                                         reject(false)                                     }                                 });                             }                         }                     });                 }             }         });     }).catch((e) => {}) }  module.exports = {     loadCity,     getLocation,     getLocal,     getLocation,     selectLocation,     calculateDistance } 

相关内容

热门资讯

围绕透视问题!决战卡五星游戏辅... 围绕透视问题!决战卡五星游戏辅助器,一起宁德钓蟹辅助,曝光教程(都是真的是有挂)-哔哩哔哩1、决战卡...
最新技巧!小程序功夫川科技(辅... 最新技巧!小程序功夫川科技(辅助挂)开挂透视辅助神器(都是存在有挂)-哔哩哔哩;1、许多玩家不知道小...
目前来看!奇迹免费自动挂机脚本... 目前来看!奇迹免费自动挂机脚本,填的那款辅助视频,攻略方法(一直有挂)-哔哩哔哩1、目前来看!奇迹免...
实测教程!腾威互娱破解辅助工具... 实测教程!腾威互娱破解辅助工具(辅助挂)开挂透视辅助神器(一贯有挂)-哔哩哔哩腾威互娱破解辅助工具是...
网友热议!四川蜀山辅助软件下载... 网友热议!四川蜀山辅助软件下载,传送屋辅助k,wpk教程(确实存在有挂)-哔哩哔哩1、许多玩家不知道...
盘点一款!河洛杠次胜率辅助器(... 盘点一款!河洛杠次胜率辅助器(辅助挂)开挂透视辅助攻略(本来是真的挂)-哔哩哔哩1、该软件可以轻松地...
更值得关注的是!丽水跑得快辅助... 更值得关注的是!丽水跑得快辅助工具,友友联盟辅助脚本,总结教程(切实是有挂)-哔哩哔哩更值得关注的是...
2024教程!新九方科技(辅助... 2024教程!新九方科技(辅助挂)开挂透视辅助攻略(一贯是有挂)-哔哩哔哩该软件可以轻松地帮助玩家将...
受玩家影响!皮皮跑子胡子,蜀山... 受玩家影响!皮皮跑子胡子,蜀山四川小程序破解版,2025新版教程(一贯是真的挂)-哔哩哔哩1、实时蜀...
一分钟教你!新星游辅助软件(辅... 一分钟教你!新星游辅助软件(辅助挂)开挂透视辅助神器(切实真的是有挂)-哔哩哔哩一分钟教你!新星游辅...