1、设置是否开启位置共享开关
使用微信自带的组件,switch开关:
微信开发者文档中改开关的属性:
https://developers.weixin.qq.com/miniprogram/dev/component/switch.html
<view>
<text>开启共享位置:</text>
<switch checked="{{isLocation}}" bindchange="switchChange"/>
</view>
2、向用户询问授权:
在app.json中添加如下字段,在调用wx.getLocation
时会向用户询问授权
"permission": {
"scope.userLocation": {
"desc": "你的位置信息将用于小程序位置接口的效果展示"
}
},
3、使用API获得用户当前位置
使用wx.getLocation
,在获得经纬度后对页面中的数据进行更新
wx.getLocation({
type: 'gcj02',
success: (res)=> {
const latitude = res.latitude
const longitude = res.longitude
this.setData({
longitude,
latitude
});
this.getNearUsers();
}
})
4、使用微信中的<map>
组件显示自己的定位
微信开发者文档中的属性介绍:
https://developers.weixin.qq.com/miniprogram/dev/component/map.html
必填属性:
(1)经纬度:
<map id="map" longitude="{{ longitude }}" latitude="{{ latitude }}"></map>
推荐填写:
(1)show-location
:显示自己的位置
其他属性的使用可以参考文档自行进行调试