位置
getLocation
TIP
該API使用方法為: wx.getLocation(Object object)
TIP
- 若使用該接口,則需要在 app.json 中進行聲明,否則將無法正常使用該接口。
- 呼叫前需要「使用者授權」 scope.userLocation。
- 功能說明: 取得目前的地理位置、速度。當使用者離開小程序後,此介面無法呼叫。
- 參數及說明: Object object。
| 内容 | 類型 | 預設值 | 必填 | 說明 |
|---|---|---|---|---|
| type | string | wgs84 | 否 | wgs84 傳回 gps 座標,gcj02 傳回可用於 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 |
- 示例代碼:
js
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座標。
- 使用協力廠商服務進行逆地址解析時,請確認協力廠商服務默認的坐標系,正確進行座標轉換。
getFuzzyLocation
TIP
該API使用方法為: wx.getFuzzyLocation(Object object)
TIP
該API小程序支持,小遊戲暫不支持
- 功能說明: 取得目前的模糊地理位置。
- 參數及說明: Object object。
| 内容 | 類型 | 預設值 | 必填 | 說明 |
|---|---|---|---|---|
| type | string | wgs84 | 否 | wgs84 傳回 gps 座標,gcj02 傳回可用於 wx.openLocation 的座標 |
| success | Function | - | 否 | 接口調用成功的回呼函數 |
| fail | Function | - | 否 | 接口調用失敗的回呼函數 |
| complete | Function | - | 否 | 接口調用結束的回呼函數(無論成功與否都執行) |
- object.success回呼函數參數: Object res
| 内容 | 類型 | 說明 |
|---|---|---|
| latitude | number | 緯度,範圍為-90~90,負數表示南緯 |
| longitude | number | 經度,範圍為-180~180,負數表示西經 |
- 示例代碼:
js
wx.getFuzzyLocation({
type: "wgs84",
success(res) {
const latitude = res.latitude;
const longitude = res.longitude;
},
});