微信小程序获取当前位置的详细步骤

作者:早起的年轻人 时间:2024-04-08 10:52:09 

微信小程序获取位置信息的方式有两种,一种是调用微信官方的接口来获取,如getLocation,这种方式只能获取经纬度
微信官方文档

https://developers.weixin.qq.com/miniprogram/dev/api/location/wx.getLocation.html

微信小程序获取当前位置的详细步骤

另一种是使用的第三方平台的,比如本文章使用的是 腾讯地图

微信小程序JavaScript SDK / 开发指南 / 入门及使用限制-开发文档

https://lbs.qq.com/miniProgram/jsSdk/jsSdkGuide/jsSdkOverview

1 腾讯位置开发基本步骤

1.1 申请开发者密钥(key)

申请密钥 :登录腾讯开发者平台,然后创建应用,如下图

微信小程序获取当前位置的详细步骤

开通webserviceAPI服务:控制台 ->应用管理 -> 我的应用 ->添加key-> 勾选WebServiceAPI -> 保存

微信小程序获取当前位置的详细步骤

(小程序SDK需要用到webserviceAPI的部分服务,所以使用该功能的KEY需要具备相应的权限)

1.2 下载微信小程序JavaScriptSDK

下载微信小程序JavaScriptSDK

https://mapapi.qq.com/web/miniprogram/JSSDK/qqmap-wx-jssdk1.2.zip

下载后解压,拷贝到微信小程序项目中

微信小程序获取当前位置的详细步骤

1.3 安全域名设置

安全域名设置,在小程序管理后台 -> 开发 -> 开发管理 -> 开发设置 -> “服务器域名” 中设置request合法域名,添加

https://apis.map.qq.com

微信小程序获取当前位置的详细步骤

1.4 微信小程序设置隐私权限

在app.json 文本中添加

"permission": {
   "scope.userLocation": {
     "desc": "小程序需要使用您的位置信息 已确认您的采样地址"
   }
 },
 "requiredPrivateInfos": [
   "getLocation"
 ],

getLocation 是使用微信接口来获取经纬度时使用,需要申请调用权限。

微信小程序获取当前位置的详细步骤

2 获取位置信息

核心代码如下:

// 引入SDK核心类,js文件根据自己业务,位置可自行放置
var QQMapWX = require('../../libs/qqmap-wx-jssdk.js');
var qqmapsdk;
Page({
onLoad: function () {
       // 实例化API核心类
       qqmapsdk = new QQMapWX({
           key: '申请的key'
       });
   },
   onShow: function () {
       // 调用接口
       qqmapsdk.reverseGeocoder({
           success: function (res) {
               let result = res.result;
               console.log(res.status, res.message);
           },
           fail: function (res) {
               console.log(res.status, res.message);
           },
           complete: function (res) {
           console.log(res.status, res.message);
       }
    });
 }
})

3 权限问题

当用户第一次进入页面获取位位置信息时,小程序会弹出请求位置权限申请,如果用户点击了拒绝权限,那下次进入时,将不会再次弹出权限申请,所以这个过程需要开发者来维护处理一下。

如果用户拒绝过,再次进入后,弹框提示用户开启权限

//定位方法
 initLocationPersmiss: function () {
   var _this = this;
   wx.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) {
         //未授权
         wx.showModal({
           title: '请求授权当前位置',
           content: '需要获取您的地理位置,请确认授权',
           success: function (res) {
             if (res.cancel) {
               //取消授权
               wx.showToast({
                 title: '拒绝授权 暂时无法使用本功能',
                 icon: 'none',
                 duration: 1000
               })
             } else if (res.confirm) {
               //确定授权,通过wx.openSetting发起授权请求
               wx.openSetting({
                 success: function (res) {
                   if (res.authSetting["scope.userLocation"] == true) {
                     wx.showToast({
                       title: '授权成功',
                       icon: 'success',
                       duration: 1000
                     })
                     //再次授权,调用wx.getLocation的API
                     _this.initGetLocationFlunction();
                   } else {
                     wx.showToast({
                       title: '授权失败',
                       icon: 'none',
                       duration: 1000
                     })
                   }
                 }
               })
             }
           }
         })
       } else if (res.authSetting['scope.userLocation'] == undefined) {
         //用户首次进入页面,调用wx.getLocation的API
         _this.initGetLocationFlunction();
       } else {
         console.log('授权成功')
         //调用wx.getLocation的API
         _this.initGetLocationFlunction();
       }
     }
   })
},

获取位置的请求

initGetLocationFlunction(isRefresh){
   let that = this;
   this.setData({isUpdateLocatin:true})
   qqmapsdk.reverseGeocoder({
     success: function(res) {
       let result = res.result;
       console.log(res);
     },
     fail: function(res) {
       console.log(res.status, res.message);
},
     complete: function(res) {
       console.log(res.status, res.message);
     }
   })
 },

完毕

来源:https://blog.csdn.net/zl18603543572/article/details/127570469

标签:微信小程序,当前位置
0
投稿

猜你喜欢

  • 详解python如何调用C/C++底层库与互相传值

    2022-02-25 07:18:00
  • 提高MYSQL查询效率的三个有效的尝试

    2009-02-27 16:08:00
  • Pytho的HTTP交互httpx包模块使用详解

    2022-11-17 06:03:29
  • vue中watch监听不到变化的解决

    2024-04-30 10:41:33
  • asp自带的内存缓存 application

    2011-03-09 11:18:00
  • python子类如何继承父类的实例变量

    2022-05-07 08:41:26
  • 详解Python3中setuptools、Pip安装教程

    2023-12-30 11:49:40
  • SQL Data Services将成为云中完整的数据库

    2009-03-25 12:28:00
  • FileSystemObject 示例代码

    2008-10-24 08:54:00
  • django将网络中的图片,保存成model中的ImageField的实例

    2023-12-23 01:11:33
  • vue使用element-resize-detector监听元素宽度变化方式

    2024-05-29 22:29:26
  • Python使用Selenium、PhantomJS爬取动态渲染页面

    2023-12-20 22:24:55
  • PyQt5+Pycharm安装和配置图文教程详解

    2022-12-20 08:50:26
  • Go语言文件读写操作案例详解

    2024-02-03 16:46:33
  • 通过T-SQL语句实现数据库备份与还原的代码

    2024-01-12 15:20:50
  • pytorch中的embedding词向量的使用方法

    2022-03-25 09:05:27
  • Mysql脏页flush及收缩表空间原理解析

    2024-01-21 23:07:25
  • scrapy-redis的安装部署步骤讲解

    2023-03-18 11:53:41
  • PHP适配器模式Adapter Pattern的使用介绍

    2023-06-10 12:28:21
  • 解决缩小图标变样问题

    2007-10-08 19:13:00
  • asp之家 网络编程 m.aspxhome.com