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.latitudeconst longitude = res.longitudeconst speed = res.speedconst accuracy = res.accuracytemp_this.setData({latitude: latitude,longitude: longitude})let map_context = wx.createMapContext('my_map', temp_this);map_context.moveToLocation({longitude:longitude, latitude:latitude});}});} })
4. 测试
![](https://img-blog.csdnimg.cn/20201127154345845.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ0NzgzMjgz,size_16,color_FFFFFF,t_70)