1. demo01.wxml
<view>
<button type="default" bindtap="get_location">get position</button>
</view>
<view>
<map id="my_map" latitude="{
{latitude}}" longitude="{
{longitude}}" show-location="true" style="width: 400px"></map>
</view>
2. 配置app.json
{
"pages": [
"pages/demo01/demo01"
],
"permission": {
"scope.userLocation": {
"desc": "你的位置信息将用于小程序位置接口的效果展示"
}
}
}
3. demo01.js
Page({
data: {
longitude: 0,
latitude: 0
},
onLoad: function (options) {
},
onReady: function () {
},
onShow: function () {
},
onHide: function () {
},
onUnload: function () {
},
onPullDownRefresh: function () {
},
onReachBottom: function () {
},
onShareAppMessage: function () {
},
get_location: function() {
let temp_this = this;
wx.getLocation({
type: 'wgs84',
success (res) {
const latitude = res.latitude
const longitude = res.longitude
const speed = res.speed
const accuracy = res.accuracy
temp_this.setData({
latitude: latitude,
longitude: longitude
})
let map_context = wx.createMapContext('my_map', temp_this);
map_context.moveToLocation({
longitude:longitude, latitude:latitude});
}
});
}
})
4. 测试