微信小程序API~地理位置location
生活随笔
收集整理的這篇文章主要介紹了
微信小程序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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 必须使用列别名命名此表达式_lambda
- 下一篇: java8笔记: sorted()之正序