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 } 

相关内容

热门资讯

透视辅助!边锋斗地主微信小程序... 透视辅助!边锋斗地主微信小程序插件(辅助挂)其实是有挂(详细辅助新2025版)边锋斗地主微信小程序插...
wepok软件透明挂!wopo... wepok软件透明挂!wopoker系统机制,(wEpOke)一向有挂(有挂技巧);亲真的是有正版授...
透视辅助!wepoker内置辅... 透视辅助!wepoker内置辅助器(辅助挂)一直有挂(详细辅助玩家教程)亲,关键说明,wepoker...
WePoKe外 挂!德扑ai操... WePoKe外 挂!德扑ai操作,(wepoKE)原先是真的有挂(有挂软件);亲真的是有正版授权,小...
透视辅助!盛世游戏透视软件(辅... 透视辅助!盛世游戏透视软件(辅助挂)果然存在有挂(详细辅助教你攻略)盛世游戏透视软件是一种具有地方特...
wepoke的确有挂!微扑克超... wepoke的确有挂!微扑克超级统计,(WePoKer)一贯真的有挂(有挂透视);亲,其实确实真的有...
透视辅助!兴动互娱软件下载(辅... 透视辅助!兴动互娱软件下载(辅助挂)果然存在有挂(详细辅助黑科技教程)兴动互娱软件下载辅助器中分为三...
wepoke辅助德之星!德扑软... wepoke辅助德之星!德扑软件开发,(wepOke)确实是真的有挂(有挂教程);免费wepoke辅...
透视辅助!陕西三代辅助(辅助挂... 透视辅助!陕西三代辅助(辅助挂)一贯是有挂(详细辅助AI教程);1、在陕西三代辅助ai机器人技巧中,...
aapoker透明挂!德州ai... aapoker透明挂!德州ai辅助神器机器人,(wepOke)素来是有挂(有挂神器),亲,有的,ai...