天天看點

微信小程式定位總結

wx.getLocation(Object object)

調用前需要 使用者授權 scope.userLocation

擷取目前的地理位置、速度。當使用者離開小程式後,此接口無法調用。

微信小程式定位總結
微信小程式定位總結
wx.getLocation({
  type: 'wgs84',
  success(res) {
    const latitude = res.latitude
    const longitude = res.longitude
    const speed = res.speed
    const accuracy = res.accuracy
  }
})
           
微信小程式定位總結

注意

工具中定位模拟使用IP定位,可能會有一定誤差。且工具目前僅支援 gcj02 坐标。

使用第三方服務進行逆位址解析時,請确認第三方服務預設的坐标系,正确進行坐标轉換。

wx.openLocation(Object object)

使用微信内置地圖檢視位置

微信小程式定位總結
wx.getLocation({
  type: 'gcj02', // 傳回可以用于wx.openLocation的經緯度
  success(res) {
    const latitude = res.latitude
    const longitude = res.longitude
    wx.openLocation({
      latitude,
      longitude,
      scale: 18
    })
  }
})
           

wx.chooseLocation(Object object)

           調用前需要 使用者授權 scope.userLocation

打開地圖選擇位置。

chooseLocation() {
    const that = this
    wx.chooseLocation({
          success(res) {
            console.log(res)
            that.setData({
              hasLocation: true,
              location: formatLocation(res.longitude, res.latitude),
              locationAddress: res.address
            })
          }
        })
}
           
微信小程式定位總結