获取当前的地理位置、速度----wx.getLocation(Object object)
1、官方文档及注意事项
wx.getLocation(Object object)官方文档
2、未授权会给出以下提示:
3、授权后会给出以下提示:
4、授权
app.json
与pages同级:
"permission": {
"scope.userLocation": {
"desc": "你的位置信息将用于小程序位置接口的效果展示"
}
}
5、wxml
<text>pages/test/test.wxml</text>
<view class="container">
<button bindtap='getLocation'>获取位置</button>
<view wx:if="{{latitude !=''}}">
<view>纬度:{{latitude}}</view>
<view>经度:{{longitude}}</view>
<view>速度:{{speed}}</view>
<view>位置的精确度:{{accuracy}}</view>
<view>高度:{{altitude}}</view>
<view>垂直精度:{{accuracy}}</view>
<view>水平精度:{{accuracy}}</view>
</view>
</view>
6、js
// pages/test/test.js
//获取应用实例
const app = getApp()
Page({
data: {
latitude: '',
longitude: '',
speed: '',
accuracy: '',
altitude: '',
verticalAccuracy: '',
horizontalAccuracy: ''
},
getLocation: function () {
var _this = this;
wx.getLocation({
type: 'wgs84',
altitude: true,//传入 true 会返回高度信息,由于获取高度需要较高精确度,会减慢接口返回速度。传入false就把altitude属性删掉
success: function (res) {
var latitude = res.latitude
var longitude = res.longitude
var speed = res.speed
var accuracy = res.accuracy
var altitude = res.altitude
var verticalAccuracy = res.verticalAccuracy
var horizontalAccuracy = res.horizontalAccuracy
_this.setData({
latitude: latitude,
longitude: longitude,
speed: speed,
accuracy: accuracy,
altitude: altitude,
verticalAccuracy: verticalAccuracy,
horizontalAccuracy: horizontalAccuracy
})
},
complete :function(r){
console.log(r)
console.log(222)
}
})
}
})