微信小程序开发:获取用户位置信息并显示到地图上
"permission": {
"": {
"desc": "你的位置信息将用于小程序位置接口的效果展示" // 小程序获取权限时展示的接口用途说明
}
}


<map id="map" longitude="{{longitude}}" latitude="{{latitude}}" scale="14" markers="{{markers}}" style="position: fixed;width: 100%;height: 100%;">map>
https://developers.weixin.qq.com/miniprogram/dev/component/map.html
data: {
longitude:"",
latitude:"",
markers:[]
},
getMyLocation:function(){
var that=this;
({
type: 'wgs84',
success (res) {
console.log(res);
console.log("1",);
({
latitude:res.latitude,
longitude:res.longitude,
markers: [{
iconPath: "/images/",
id: 0,
latitude: res.latitude,
longitude: res.longitude
}],
});
console.log("2",);
}
})
}
onLoad: function (options) {
var that=this;
//获取用户当前的授权状态
({
success(res) {
//若用户没有授权地理位置
if (!['']) {
//在调用需授权 API 之前,提前向用户发起授权请求
({
scope: '',
//用户同意授权
success () {
// 用户已经同意小程序使用地理位置,后续调用 接口不会弹窗询问
();
},
////用户不同意授权
fail(){
({
title: '提示',
content: '此功能需获取位置信息,请授权',
success: function (res) {
if (res.confirm == false) {
return false;
}
({
success(res) {
//如果再次拒绝则返回页面并提示
if (!['']) {
({
title: '此功能需获取位置信息,请重新设置',
duration: 3000,
icon: 'none'
})
} else {
//允许授权,调用地图
()
}
}
})
}
})
}
})
}
else{
();
}
}
})
},





THE END