data() {
return {
qqmapsdk: null,
}
},
onLoad(options) {
// key 从腾讯地图申请
this.qqmapsdk = new QQMapWX({
key: "LZCBZ-UIXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
})
this.mapsdk()
},
methods:{
mapsdk() {
var _this = this
uni.showLoading({
title: "获取位置中"
})
this.qqmapsdk.reverseGeocoder({
success(res) {
if (res.status == 0) {
_this.address = res.result.address + res.result.formatted_addresses.recommend
}
uni.hideLoading()
},
fail(err) {
// console.log(err);
_this.showLocationWarning(err.message)
uni.hideLoading()
},
})
},
// 这个方法需要reverseGeocoder失败的回调传下来的err.message 来判断是否打开定位,是否授权
showLocationWarning (msg) {
if (msg) {
if (msg.indexOf('频繁调用') >= 0) {
uni.showToast({
title: '请勿频繁定位',
icon: 'none'
})
return;
}
if (msg.indexOf('ERROR_NOCELL&WIFI_LOCATIONSWITCHOFF') >= 0) {
uni.showModal({
content: '请开启手机定位服务',
showCancel: false
})
return;
}
// if (msg == 'getLocation:fail auth deny') {
// uni.showToast({
// title: '请勿频繁定位',
// icon: 'none'
// })
// }
}
var that = this;
uni.getSetting({
success(res) {
if (!res.authSetting["scope.userLocation"]) {
uni.showModal({
title: "授权位置信息",
content: "请确认授权位置信息,否则无法定位",
success: function (msg) {
if (msg.confirm) {
uni.openSetting({
success: function (e) {
if (e.authSetting["scope.userLocation"] === true) {
uni.showToast({
title: "授权成功",
icon: "success",
duration: 1000,
});
that.mapsdk();
}
},
});
} else if (msg.cancel) {
uni.showToast({
title: "授权位置失败",
icon: "none",
duration: 1000,
});
}
},
});
} else {
uni.showToast({
title: "无法获取当前位置",
icon: "error",
duration: 5000,
});
}
},
});
},
}
获取位置信息需要在manifest.json中打开
从腾讯地图下载的qqmap-wx-jssdk.js放在自己的js文件件中
在那个页面需要就引用就来使用,然后可根据代码片段使用即可