位置
getLocation
TIP
該API使用方法為: wx.getLocation(Object object)
TIP
- 若使用該接口,需要在app.json中進行聲明,否則將無法正常使用該接口。
- 調用前需要'用戶授權'scope.userLocation。
功能說明: 獲取當前的地理位置、速度。 當用戶離開小程序後,此接口無法調用。 高頻率調用會導致耗電,如有需要可使用持續定位接口wx.onLocationChange
參數及說明: Object object。
内容 類型 預設值 必填 說明 type string wgs84 否 wgs84返回gps座標,返回可用於wx.openLocation的座標 altitude string false 否 傳入true會返回高度資訊,由於獲取高度需要較高精確度,會减慢接口返回速度 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 verticalAccuracy number 垂直精度,單位m(Android無法獲取,返回0) horizontalAccuracy number 水准精度,單位m 示例代碼:
wx.getLocation({
type: 'gcj02',
success(res) {
const latitude = res.latitude
const longitude = res.longitude
const speed = res.speed
const accuracy = res.accuracy
}
})TIP
- 工具中定位類比使用IP定位,可能會有一定誤差。 且工具現時僅支持gcj02座標。
- 使用協力廠商服務進行逆地址解析時,請確認協力廠商服務默認的坐標系,正確進行座標轉換。
choosePoi
TIP
該API使用方法為: wx.choosePoi(Object object)
TIP
- 若使用該接口,需要在app.json中進行聲明scope.userLocation,否則將無法正常使用該接口。
功能說明: 打開POI清單選擇位置,支持模糊定位(精確到市)和精確定位混選。
參數及說明: Object object。
内容 類型 預設值 必填 說明 success Function - 否 接口調用成功的回呼函數 fail Function - 否 接口調用失敗的回呼函數 complete Function - 否 接口調用結束的回呼函數(無論成功與否都執行) object.success回呼函數參數: Object res。
内容 類型 說明 type number 選擇城市時,值為1,選擇精確位置時,值為2 city number 都市名稱 name string 位置名稱 address string 詳細地址 latitude number 緯度,浮點數,範圍為-90~90,負數表示南緯。 (即將廢棄) longitude number 經度,浮點數,範圍為-180~180,負數表示西經。 (即將廢棄)
chooseLocation
TIP
該API使用方法為: wx.chooseLocation(Object object)
TIP
- 若使用該接口,需要在app.json中進行聲明scope.userLocation,否則將無法正常使用該接口。
功能說明: 打開地圖選擇位置
參數及說明: Object object。
内容 類型 預設值 必填 說明 type string tencent 否 騰訊地圖tencent,穀歌地圖google,僅IDE支持 latitude number - 否 目標地緯度 longitude number - 否 目標地經度 success Function - 否 接口調用成功的回呼函數 fail Function - 否 接口調用失敗的回呼函數 complete Function - 否 接口調用結束的回呼函數(無論成功與否都執行) object.success回呼函數參數: Object res。
内容 類型 說明 name string 位置名稱 address string 詳細地址 latitude number 緯度,浮點數,範圍為-90~90,負數表示南緯。 longitude number 經度,浮點數,範圍為-180~180,負數表示西經。
stopLocationUpdate
TIP
該API使用方法為: wx.stopLocationUpdate(Object object)
- 功能說明: 關閉監聽實时位置變化,前後臺都停止消息接收。
- 參數及說明: Object object。
内容 類型 預設值 必填 說明 success Function - 否 接口調用成功的回呼函數 fail Function - 否 接口調用失敗的回呼函數 complete Function - 否 接口調用結束的回呼函數(無論成功與否都執行)
startLocationUpdateBackground
TIP
該API使用方法為: wx.startLocationUpdateBackground(Object object)
- 功能說明: 開啟小程序進入前後臺時均接收位置消息,需引導用戶開啟'授權'。 授權以後,小程序在運行中或進入後臺均可接受位置消息變化。
- 參數及說明: Object object。
内容 類型 預設值 必填 說明 type string gcj02 否 wgs84返回gps座標,返回可用於wx.openLocation的座標 success Function - 否 接口調用成功的回呼函數 fail Function - 否 接口調用失敗的回呼函數 complete Function - 否 接口調用結束的回呼函數(無論成功與否都執行)
startLocationUpdate
TIP
該API使用方法為: wx.startLocationUpdate(Object object)
TIP
- 若使用該接口,需要在app.json中進行聲明scope.userLocation,否則將無法正常使用該接口。
- 功能說明: 開啟小程序進入前臺時接收位置消息。
- 參數及說明: Object object。
内容 類型 預設值 必填 說明 type string gcj02 否 wgs84返回gps座標,返回可用於wx.openLocation的座標 success Function - 否 接口調用成功的回呼函數 fail Function - 否 接口調用失敗的回呼函數 complete Function - 否 接口調用結束的回呼函數(無論成功與否都執行)
openLocation
TIP
該API使用方法為: wx.openLocation(Object object)
功能說明: 使用內寘地圖查看位置
參數及說明: Object object。
内容 類型 預設值 必填 說明 type string tencent 否 騰訊地圖tencent,穀歌地圖google,僅IDE支持 latitude number - 是 緯度,範圍為-90~90,負數表示南緯。 longitude number - 是 經度,範圍為-180~180,負數表示西經。 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
})
}
})onLocationChangeError
TIP
該API使用方法為: wx.onLocationChangeError(function listener)
功能說明: 監聽持續定位接口返回失敗時觸發。
參數及說明: function callback的回呼函數,參數Object res如下:
内容 類型 說明 errCode number 錯誤碼
onLocationChange
TIP
該API使用方法為: wx.onLocationChange(function listener)
功能說明: 監聽實时地理位置變化事件,需結合 wx.startLocationUpdateBackground,wx.startLocationUpdate 使用
參數及說明: function callback,實时地理位置變化事件的監聽函數,參數Object res如下:
内容 類型 說明 latitude number 緯度,範圍為-90~90,負數表示南緯 longitude number 經度,範圍為-180~180,負數表示西經 speed number 速度,單位m/s accuracy number 位置的精確度 altitude number 高度,單位m verticalAccuracy number 垂直精度,單位m(Android無法獲取,返回0) horizontalAccuracy number 水准精度,單位m 示例代碼:
const _locationChangeFn = function(res) {
console.log('location change', res)
}
wx.onLocationChange(_locationChangeFn)
wx.offLocationChange(_locationChangeFn)offLocationChangeError
TIP
該API使用方法為: wx.offLocationChangeError(function listener)
功能說明: 移除持續定位接口返回失敗時觸發的監聽函數。
參數及說明: function listener, onLocationChangeError傳入的監聽函數。 不傳此參數則移除所有監聽函數。
示例代碼:
const listener = function (res) { console.log(res) }
wx.onLocationChangeError(listener)
wx.offLocationChangeError(listener) // Pass the same function object used for listening.offLocationChange
TIP
該API使用方法為: wx.offLocationChange(function listener)
功能說明: 移除實时地理位置變化事件的監聽函數。
參數及說明: function listener, onLocationChange傳入的監聽函數。 不傳此參數則移除所有監聽函數。
示例代碼:
const listener = function (res) { console.log(res) }
wx.onLocationChange(listener)
wx.offLocationChange(listener) // Pass the same function object used for listening.getFuzzyLocation
TIP
該API使用方法為: wx.getFuzzyLocation(Object object)
功能說明: 獲取當前的模糊地理位置
參數及說明: Object object。
内容 類型 預設值 必填 說明 type string wgs84 否 wgs84返回gps座標,返回可用於wx.openLocation的座標 success Function - 否 接口調用成功的回呼函數 fail Function - 否 接口調用失敗的回呼函數 complete Function - 否 接口調用結束的回呼函數(無論成功與否都執行) object.success回呼函數參數: Object res。
内容 類型 說明 latitude number 緯度,範圍為-90~90,負數表示南緯 longitude number 經度,範圍為-180~180,負數表示西經 示例代碼:
wx.getFuzzyLocation({
type: 'wgs84',
success (res) {
const latitude = res.latitude
const longitude = res.longitude
}
})