当前位置:   article > 正文

uniapp小程序获取位置信息_uniapp小程序获取当前的位置和输入地点名字获取位置

uniapp小程序获取当前的位置和输入地点名字获取位置

地理位置接口新增与相关流程调整

一、配置位置信息

1、需配置的接口列表 

2、接口权限开通

以上 8 个接口需完成准入开通流程

(1)普通开发者:需要在 “小程序管理后台 -「开发」-「开发管理」-「接口设置」” 中完成权限申请;

(2)第三方开发者:可通过 apply_privacy_interface 接口完成权限申请。

3、配置规则

(1)在代码中使用的地理位置相关接口(共计 8 个,见表1),开发者均需要在 manifest.json 中 requiredPrivateInfos 配置项中声明,代码格式如下:

(2)表1中模糊位置信息(序号1)和精确位置信息(序号2-5)是互斥的,即声明了模糊位置信息就无法声明精确位置信息。若同时声明模糊位置信息和精确位置信息,则在编译代码时出现错误;

(3)注意:自 2022 年 7 月 14 日后发布的小程序,如果未在 app.json 中声明表1中的相关接口,则小程序调用这些接口(表1)时会出现错误,在 2022 年 7 月 14 日之前发布的小程序不受影响 

二、使用

1、地图选择位置(chooseLocation)

(1)配置

manifest.json

"requiredPrivateInfos" : [ "chooseLocation"],

(2)使用说明

  1. uni.chooseLocation({
  2. success: function (res) {
  3. console.log('位置名称:' + res.name);
  4. console.log('详细地址:' + res.address);
  5. console.log('纬度:' + res.latitude);
  6. console.log('经度:' + res.longitude);
  7. }
  8. });

2、模糊位置信息(getFuzzyLocation)

(1)配置

manifest.json

  1. "permission": {
  2. "scope.userFuzzyLocation": {
  3. "desc": "你的位置信息将用于小程序位置接口的效果展示"
  4. }
  5. },
  6. "requiredPrivateInfos": [
  7. "getFuzzyLocation"
  8. ],

(2)使用说明

  1. uni.getSetting({
  2. success: (res) => {
  3. if (res.authSetting['scope.userFuzzyLocation']) {
  4. /* 用户授权时走这里 */
  5. this.getFuzzyLocationInfo()
  6. return
  7. }
  8. if (res.authSetting['scope.userFuzzyLocation'] === undefined) {
  9. /* 用户未授权时走这里 */
  10. uni.openSetting({
  11. success: (res) => {
  12. if (res.authSetting["scope.userFuzzyLocation"]) {
  13. this.getFuzzyLocationInfo()
  14. }
  15. }
  16. })
  17. } else {
  18. /* 用户拒绝了授权后走这里 */
  19. uni.openSetting({
  20. success: (res) => {
  21. if (res.authSetting["scope.userFuzzyLocation"]) {
  22. this.getFuzzyLocationInfo()
  23. }
  24. }
  25. })
  26. }
  27. },
  28. })

说明:

uni.openSetting:调起客户端小程序设置界面,返回用户设置的操作结果。 

uni.getSetting:获取用户的当前设置。

authSetting:用户授权结果,其中 key 为 scope 值,value 为 Boolean 值,表示用户是否允许授权

  1. // 获取模糊地址
  2. getFuzzyLocationInfo() {
  3. wx.getFuzzyLocation({
  4. type: 'wgs84',
  5. success(res) {
  6. console.log('当前位置的经度:' + res.longitude);
  7. console.log('当前位置的纬度:' + res.latitude);
  8. }
  9. })
  10. },

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/215778
推荐阅读
相关标签
  

闽ICP备14008679号