微信小程序获取当前位置与跳转第三方地图

1. 先在app.json添加配置

 "permission": {
    
    
    "scope.userLocation": {
    
    
      "desc": "你的位置信息将用于小程序位置接口的效果展示"
    }
  },
  "requiredPrivateInfos": [
    "getLocation",
    "chooseLocation"
  ],

2. 获取定位权限,打开地址选点

 wx.showLoading({
    
    
      title: '正在打开',
      mask:"true"
    })
    wx.getLocation({
    
        //获取位置权限
      type: 'gcj02',
      isHighAccuracy:true,
      highAccuracyExpireTime:4000,
      success(res) {
    
    
        console.log(res)
         var latitude = res.latitude // 纬度
         var longitude = res.longitude // 经度
         wx.chooseLocation({
    
       //打开地址选点
          latitude,
          longitude,
          success: function (data) {
    
    
            console.log(data)
          },
          fail(res) {
    
    
            console.log(res) // getLocation:fail the api need to be declared in the requiredPrivateInfos field in app.json
          },
          complete(){
    
    
            wx.hideLoading()
          }
        })
      },
      fail(res) {
    
    
        wx.hideLoading()
        wx.openSetting()  //用户拒绝位置权限后打开设置
      }
    })

3. 如果要打开跳转第三方地图的内置地图

  wx.openLocation({
    
    
          latitude,  //经度
          longitude, //维度
          name: '自提位置',  // 位置名
          address:"第十六届可能安防监控",  // 要去的地址详情说明
          scale: 1,   // 地图缩放级别,整形值,范围从1~28。默认为最大
          success: function (data) {
    
    
            console.log(data)
          },
          fail(res) {
    
    
            console.log(res) // getLocation:fail the api need to be declared in the requiredPrivateInfos field in app.json
          },
          complete(){
    
    
            wx.hideLoading()
          }
        })

猜你喜欢

转载自blog.csdn.net/qq_44749901/article/details/128480513