uniapp获取当前的地理位置【uni.getLocation(OBJECT)】
vue 2022-12-18 17:03:07

iShot_2022-12-18_17.12.01.png

 

uni-app获取当前的地理位置【uni.getLocation(OBJECT)】

在 manifest.json中配置 permission

iShot_2022-12-18_17.04.44.png

也可以直接在配置文件中添加:

JavaScript Code复制内容到剪贴板
  1. /* 小程序特有相关 */  
  2. "mp-weixin" : {  
  3.     ...,  
  4.     "permission" : {  
  5.         "scope.userLocation" : {  
  6.             "desc" : "请点击“确定”以获取当前位置信息"  
  7.         }  
  8.     },
  9.    "requiredPrivateInfos": ["getLocation"]  
  10. },  

 

在需要的页面写:

v2:

JavaScript Code复制内容到剪贴板
  1. getLocation: async function () {  
  2.     // 使用promise 判断地理位置是否授权  
  3.     return new Promise((resolve, reject) => {  
  4.         uni.getSystemInfo({  
  5.             success({  
  6.                 locationEnabled,  
  7.                 locationAuthorized  
  8.             }) {  
  9.                 // locationEnabled 判断手机定位服务是否开启  
  10.                 // locationAuthorized 判断定位服务是否允许微信授权  
  11.                 if (!locationEnabled && !locationAuthorized) {  
  12.                     // GPS未开启 与 GPS未给微信授权定位服务  
  13.                     uni.showModal({  
  14.                         title: '需要获取地理位置',    
  15.                         content: 'GPS已开启,GPS未给微信授权定位服务',    
  16.                         showCancel: false    
  17.                     });  
  18.                     reject("GPSnotOpen");  
  19.                 } else if (locationEnabled && !locationAuthorized) {  
  20.                     // GPS已开启 与 GPS未给微信授权定位服务  
  21.                     uni.showModal({  
  22.                         title: '需要获取地理位置',    
  23.                         content: 'GPS已开启,GPS未给微信授权定位服务',    
  24.                         showCancel: false    
  25.                     });  
  26.                     reject("GPSauthorization");  
  27.                 } else if (locationEnabled && locationAuthorized) {  
  28.                     /*  
  29.                         GPS已开启 与 GPS已给微信授权定位服务 
  30.                         判断微信小程序位置信息是否开启 
  31.                     */  
  32.                     uni.authorize({  
  33.                         scope: "scope.userLocation",  
  34.                         success() {  
  35.                             // 微信小程序位置信息已开启  
  36.                             uni.getLocation({  
  37.                                 success({  
  38.                                     latitude,  
  39.                                     longitude  
  40.                                 }) {  
  41.                                     // latitude; 纬度  
  42.                                     // longitude; 经度  
  43.                                     this.longitude = longitude  
  44.                                     this.latitude = latitude  
  45.                                     resolve({  
  46.                                         latitude,  
  47.                                         longitude  
  48.                                     });  
  49.                                 }  
  50.                             });  
  51.                         },  
  52.                         fail() {  
  53.                             // 微信小程序位置信息未开启  
  54.                             uni.getSetting({  
  55.                                 success: (res) => {    
  56.                                     let authStatus = res.authSetting['scope.userLocation'];    
  57.                                     if (!authStatus) {    
  58.                                         uni.showModal({    
  59.                                             title: '需要获取地理位置',    
  60.                                             content: '需要获取您的位置信息,请在设置界面打开相关权限',    
  61.                                             success: (res) => {  
  62.                                                 if (res.confirm) {    
  63.                                                     uni.openSetting()    
  64.                                                 }  
  65.                                                 reject()  
  66.                                             }  
  67.                                         })  
  68.                                     } else {  
  69.                                         uni.showModal({  
  70.                                             title: '打卡失败',    
  71.                                             content: '无法获取位置信息',    
  72.                                             showCancel: false    
  73.                                         });  
  74.                                         reject('用户拒绝授权')  
  75.                                         // 拒绝后的回调,这个弹窗应该关掉    
  76.                                     }  
  77.                                 }  
  78.                             })  
  79.                             // uni.showModal({  
  80.                             //  title: '需要获取地理位置',    
  81.                             //  content: '微信小程序位置信息未开启',    
  82.                             //  showCancel: false    
  83.                             // });  
  84.                             // reject("weixinPositionNotOpen");  
  85.                         }  
  86.                     })  
  87.                 }  
  88.             },  
  89.             fail(err) {  
  90.                 let reg = /request:fail/;  
  91.                 if (reg.test(err.errMsg)) {  
  92.                     // 无网络  
  93.                     reject("noNetWork");  
  94.                 } else {  
  95.                     // 请求超时'  
  96.                     reject("requestTimeOut");  
  97.                 }  
  98.             }  
  99.         })  
  100.     });  
  101.       
  102. },  

 

 

v1:

JavaScript Code复制内容到剪贴板
  1. // 判断地理位置是否授权  
  2. function authorizedPositioning( callBack = ()=>{} ){  
  3.     uni.getSystemInfo({ // 获取系统信息  
  4.             success(res) {  
  5.                 let locationEnabled = res.locationEnabled; //判断手机定位服务是否开启  
  6.                 let locationAuthorized = res.locationAuthorized; //判断定位服务是否允许微信授权  
  7.                 if (locationEnabled == false || locationAuthorized == false) {  
  8.                     // GPS 未授权  
  9.                     callBack("GPSunauthorized");  
  10.                 } else {  
  11.                     // GPS 已授权 判断微信定位是否授权  
  12.                     uni.authorize({  
  13.                         scope: 'scope.userLocation',  
  14.                         success() {  
  15.                             // GPS已授权 微信定位已授权  
  16.                             uni.getLocation({  
  17.                                 type: 'gcj02',  
  18.                                 success({  
  19.                                     latitude,  
  20.                                     longitude  
  21.                                 }) {  
  22.                                     // latitude; 纬度  
  23.                                     // longitude; 经度  
  24.                                     callBack("Authorized", {  
  25.                                         latitude,  
  26.                                         longitude  
  27.                                     });  
  28.                                 }  
  29.                             });  
  30.                         },  
  31.                         fail() {  
  32.                             // GPS已授权 微信定位未授权  
  33.                             callBack("WENXINunauthorized");  
  34.                             uni.showModal({  
  35.                                 title: '未打开小程序定位',  
  36.                                 content: '找不到您的位置,请开启定位',  
  37.                                 confirmText: '开启定位',  
  38.                                 showCancel: false,  
  39.                                 success: (res) => {  
  40.                                     if (res.confirm) {  
  41.                                         uni.openSetting(); // 打开地图权限设置  
  42.                                     }  
  43.                                 }  
  44.                             });  
  45.                         }  
  46.                     });  
  47.                 }  
  48.             }  
  49.     })  
  50. }  
  51.   
  52. // 使用promise 判断地理位置是否授权  
  53. function authorizedPositioningPromise() {  
  54.     return new Promise((resolve, reject) => {  
  55.         uni.getSystemInfo({  
  56.             success({  
  57.                 locationEnabled,  
  58.                 locationAuthorized  
  59.             }) {  
  60.                 // locationEnabled 判断手机定位服务是否开启  
  61.                 // locationAuthorized 判断定位服务是否允许微信授权  
  62.                 if (!locationEnabled && !locationAuthorized) {  
  63.                     // GPS未开启 与 GPS未给微信授权定位服务  
  64.                     reject("GPSnotOpen");  
  65.                 } else if (locationEnabled && !locationAuthorized) {  
  66.                     // GPS已开启 与 GPS未给微信授权定位服务  
  67.                     reject("GPSauthorization");  
  68.                 } else if (locationEnabled && locationAuthorized) {  
  69.                     /*  
  70.                         GPS已开启 与 GPS已给微信授权定位服务 
  71.                         判断微信小程序位置信息是否开启 
  72.                     */  
  73.                     uni.authorize({  
  74.                         scope: "scope.userLocation",  
  75.                         success() {  
  76.                             // 微信小程序位置信息已开启  
  77.                             uni.getLocation({  
  78.                                 success({  
  79.                                     latitude,  
  80.                                     longitude  
  81.                                 }) {  
  82.                                     // latitude; 纬度  
  83.                                     // longitude; 经度  
  84.                                     resolve({  
  85.                                         latitude,  
  86.                                         longitude  
  87.                                     });  
  88.                                 }  
  89.                             });  
  90.                         },  
  91.                         fail() {  
  92.                             // 微信小程序位置信息未开启  
  93.                             reject("weixinPositionNotOpen");  
  94.                         }  
  95.                     })  
  96.                 }  
  97.             },  
  98.             fail(err) {  
  99.                 let reg = /request:fail/;  
  100.                 if (reg.test(err.errMsg)) {  
  101.                     // 无网络  
  102.                     reject("noNetWork");  
  103.                 } else {  
  104.                     // 请求超时'  
  105.                     reject("requestTimeOut");  
  106.                 }  
  107.             }  
  108.         })  
  109.     });  
  110. }  

 

在添了es6的语法中,加了弹出框提示:

JavaScript Code复制内容到剪贴板
  1. // 使用promise 判断地理位置是否授权    
  2. function authorizedPositioningPromise() {    
  3.                 // 使用promise 判断地理位置是否授权  
  4.                 return new Promise((resolve, reject) => {  
  5.                     uni.getSystemInfo({  
  6.                         success({  
  7.                             locationEnabled,  
  8.                             locationAuthorized  
  9.                         }) {  
  10.                             // locationEnabled 判断手机定位服务是否开启  
  11.                             // locationAuthorized 判断定位服务是否允许微信授权  
  12.                             if (!locationEnabled && !locationAuthorized) {  
  13.                                 // GPS未开启 与 GPS未给微信授权定位服务  
  14.                                 uni.showModal({  
  15.                                     title: '需要获取地理位置',    
  16.                                     content: 'GPS已开启,GPS未给微信授权定位服务',    
  17.                                     showCancel: false    
  18.                                 });  
  19.                                 reject("GPSnotOpen");  
  20.                             } else if (locationEnabled && !locationAuthorized) {  
  21.                                 // GPS已开启 与 GPS未给微信授权定位服务  
  22.                                 uni.showModal({  
  23.                                     title: '需要获取地理位置',    
  24.                                     content: 'GPS已开启,GPS未给微信授权定位服务',    
  25.                                     showCancel: false    
  26.                                 });  
  27.                                 reject("GPSauthorization");  
  28.                             } else if (locationEnabled && locationAuthorized) {  
  29.                                 /*  
  30.                                     GPS已开启 与 GPS已给微信授权定位服务 
  31.                                     判断微信小程序位置信息是否开启 
  32.                                 */  
  33.                                 uni.authorize({  
  34.                                     scope: "scope.userLocation",  
  35.                                     success() {  
  36.                                         // 微信小程序位置信息已开启  
  37.                                         uni.getLocation({  
  38.                                             success({  
  39.                                                 latitude,  
  40.                                                 longitude  
  41.                                             }) {  
  42.                                                 // latitude; 纬度  
  43.                                                 // longitude; 经度  
  44.                                                 this.longitude = longitude  
  45.                                                 this.latitude = latitude  
  46.                                                 resolve({  
  47.                                                     latitude,  
  48.                                                     longitude  
  49.                                                 });  
  50.                                             }  
  51.                                         });  
  52.                                     },  
  53.                                     fail() {  
  54.                                         // 微信小程序位置信息未开启  
  55.                                         uni.showModal({  
  56.                                             title: '需要获取地理位置',    
  57.                                             content: '微信小程序位置信息未开启',    
  58.                                             showCancel: false    
  59.                                         });  
  60.                                         reject("weixinPositionNotOpen");  
  61.                                     }  
  62.                                 })  
  63.                             }  
  64.                         },  
  65.                         fail(err) {  
  66.                             let reg = /request:fail/;  
  67.                             if (reg.test(err.errMsg)) {  
  68.                                 // 无网络  
  69.                                 reject("noNetWork");  
  70.                             } else {  
  71.                                 // 请求超时'  
  72.                                 reject("requestTimeOut");  
  73.                             }  
  74.                         }  
  75.                     })  
  76.                 });  
  77.                   
  78. }    

 

 

 

 

本文来自于:http://www.yoyo88.cn/study/vue/650.html

Powered by yoyo苏ICP备15045725号