日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

微信小程序API~地理位置location

發布時間:2023/12/13 综合教程 27 生活家
生活随笔 收集整理的這篇文章主要介紹了 微信小程序API~地理位置location 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

(1)使用微信內置地圖查看位置

wx.openLocation(Object object)

使用微信內置地圖查看位置

參數

Object object

屬性 類型 默認值 必填 說明
latitude number 緯度,范圍為-90~90,負數表示南緯。使用 gcj02 國測局坐標系
longitude number 經度,范圍為-180~180,負數表示西經。使用 gcj02 國測局坐標系
scale number 18 縮放比例,范圍5~18
name string 位置名
address string 地址的詳細說明
success function 接口調用成功的回調函數
fail function 接口調用失敗的回調函數
complete function 接口調用結束的回調函數(調用成功、失敗都會執行)

示例代碼

wx.getLocation({
 type: 'gcj02', //返回可以用于wx.openLocation的經緯度
 success (res) {
   const latitude = res.latitude
   const longitude = res.longitude
   wx.openLocation({
     latitude,
     longitude,
     scale: 18
   })
 }
})

(2)獲取當前的地理位置、速度

當用戶離開小程序后,此接口無法調用

wx.getLocation(Object object)

調用前需要用戶授權scope.userLocation

獲取當前的地理位置、速度。當用戶離開小程序后,此接口無法調用。

參數

Object object

屬性 類型 默認值 必填 說明 最低版本
type string wgs84 wgs84 返回 gps 坐標,gcj02 返回可用于 wx.openLocation 的坐標
altitude string false 傳入 true 會返回高度信息,由于獲取高度需要較高精確度,會減慢接口返回速度 1.6.0
success function 接口調用成功的回調函數
fail function 接口調用失敗的回調函數
complete function 接口調用結束的回調函數(調用成功、失敗都會執行)

object.success 回調函數

參數
Object res
屬性 類型 說明 最低版本
latitude number 緯度,范圍為 -90~90,負數表示南緯
longitude number 經度,范圍為 -180~180,負數表示西經
speed number 速度,單位 m/s
accuracy number 位置的精確度
altitude number 高度,單位 m 1.2.0
verticalAccuracy number 垂直精度,單位 m(Android 無法獲取,返回 0) 1.2.0
horizontalAccuracy number 水平精度,單位 m 1.2.0

示例代碼

wx.getLocation({
 type: 'wgs84',
 success (res) {
   const latitude = res.latitude
   const longitude = res.longitude
   const speed = res.speed
   const accuracy = res.accuracy
 }
})

注意

工具中定位模擬使用IP定位,可能會有一定誤差。且工具目前僅支持 gcj02 坐標。
使用第三方服務進行逆地址解析時,請確認第三方服務默認的坐標系,正確進行坐標轉換。

人工按鈕授權,獲取位置信息代碼:

<button bindtap="getLocation">獲取</button>

  getLocation(){
    var _this = this;
    wx.openSetting({
      success(res) {
        if (res.authSetting['scope.userLocation']) {
          wx.getLocation({
            type: 'wgs84',//默認wgs84是全球定位系統獲取的坐標,gcj02是國家測繪局給出的坐標
            success: (res) => {
              console.log('經度' + res.longitude + ',緯度' + res.latitude);
              _this.setData({
                latitude: res.latitude,
                longitude: res.longitude
              })
            }
          })
        }
      }
    })
  }

【拓展】箭頭函數this指向

談到this指向的時候箭頭函數的this指向和普通函數不一樣的,

=>this指向的是定義時this指向的對象,不會改變

function()聲明函數時的this指向會指向使用時所在的對象

所以在上面案例中,如果用交通員函數,則不用在最值前重定義this為_this

(3)打開地圖選擇位置wx.chooseLocation(Object object)

wx.chooseLocation(Object object)

調用前需要用戶授權scope.userLocation

打開地圖選擇位置。

參數

Object object

屬性 類型 默認值 必填 說明
success function 接口調用成功的回調函數
fail function 接口調用失敗的回調函數
complete function 接口調用結束的回調函數(調用成功、失敗都會執行)

object.success 回調函數

參數
Object res
屬性 類型 說明
name string 位置名稱
address string 詳細地址
latitude string 緯度,浮點數,范圍為-90~90,負數表示南緯。使用 gcj02 國測局坐標系
longitude string 經度,浮點數,范圍為-180~180,負數表示西經。使用 gcj02 國測局坐標系

.

總結

以上是生活随笔為你收集整理的微信小程序API~地理位置location的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。