关于微信小程序:微信小程序关于地址信息的接入以及自动选择当前位置

3次阅读

共计 1009 个字符,预计需要花费 3 分钟才能阅读完成。

在做收货地址的时候,通常会让用户填写或者关上地图抉择播种地址,此时就须要用到微信提供的地址 API,在应用地址 API 的时候须要注册对应的 API,而且地址 API 会常常调整,须要关注官网布告,上面就是对于地址 API 的应用。

第一局部

先看下此设置有没有关上

进入微信开发者文档,在 API 下找到地位,此处就是地位信息 API 了

在应用地位信息之前,须要在 app.json 中注册地位信息 api
此处我应用的是 wx.chooseLocation,所以在 app.json 中注册这个 api 即可

注册实现后,在页面应用

    //   抉择地址 在事件内调用这个 api 即可
      wx.chooseLocation({
        latitude: 0,
        success(res){console.log(res);  // 抉择的地址信息
        }
      })
    },

第二局部

局部 API 会弹出 须要在 app.json 中申明 permission 字段

尽管在 requiredPrivateInfos 注册过

然而还须要在 permission 中注册

此时再应用 API 即可

// 获取以后地址信息
    wx.getLocation({
        type: 'gcj02', // 返回能够用于 wx.openLocation 的经纬度
        success (res) {
          const latitude = res.latitude  // 维度
          const longitude = res.longitude  // 经度
          wx.openLocation({
            latitude,
            longitude,
            scale: 18
          })
        }
       })

关上地位信息,主动定位到以后地位

     // 地址
    addAddress(){
        let that = this
    //   抉择地址
   wx.getLocation({    // 获取以后地址信息, 地理位置、速度
    type: 'gcj02', // 返回能够用于 wx.openLocation 的经纬度
    success (res) {
      const latitude = res.latitude  // 维度
      const longitude = res.longitude  // 经度
      wx.chooseLocation({   // 通过经纬度主动定位到以后地位
        latitude,   // 维度
        longitude,  // 经度
        success(res){wx.setStorageSync('address', res.address+res.name)   
            that.setData({address : res.address+res.name  // 将以后地位信息保留,回显})
        }
      })
    }
   })
    },
正文完
 0