一、申请密钥key
- https://lbs.qq.com/ 腾讯申请地址,没注册的先注册,然后进入控制台
- 创建应用,添加key
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/04770c22bdd96ff89d26b067afa4bb19.png)
- uniapp授权
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/00a0946bd99134a6bbb58d17eb546ae7.png)
- 在腾讯地图上下载微信小程序javaScript SDK放入项目里,地址https://lbs.qq.com/miniProgram/jsSdk/jsSdkGuide/jsSdkOverview
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/0eca08a8f04bd785dd70c76552f21948.png)
- 引入项目,路径自行改
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/bb04604315b59779933362833d12352a.png)
二、项目使用代码
提示,引入可能会报错,修改如下
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/0a9784a276c65ef45f8410086b9e6d2e.png)
代码使用
import QQMapWx from '@/js_sdk/qqmap-wx-jssdk1.2/qqmap-wx-jssdk.js'
const qqmapSdk = new QQMapWx({
key: '*********'
});
getAuthorize() {
uni.authorize({
scope: 'scope.userLocation',
success: (res) => {
this.getLocation('gcj02');
},
fail: (err) => {
uni.showModal({
content: '需要授权位置信息',
confirmText: '确认授权'
}).then(res => {
if (res['confirm']) {
uni.openSetting({
success: res => {
if (res.authSetting['scope.userLocation']) {
uni.showToast({
title: '授权成功',
icon: 'none'
})
} else {
uni.showToast({
title: '授权失败',
icon: 'none'
})
}
this.getLocation('gcj02')
}
})
}
if (res['cancel']) {
this.getLocation('gcj02')
}
})
}
})
},
getLocation(type) {
uni.getLocation({
type: type,
success: (res) => {
const {
latitude,
longitude
} = res;
qqmapSdk.reverseGeocoder({
location: latitude ? `${latitude},${longitude }` : '',
sig: '**************',
success: (val) => {
console.log('这就是要获取的当前所在城市的相关信息', val)
},
fail: (err) => {
console.log('获取城市失败')
}
})
},
fail: (err) => {
uni.stopPullDownRefresh();
if (err.errMsg === 'getLocation:fail:ERROR_NOCELL&WIFI_LOCATIONSWITCHOFF' || err
.errMsg === 'getLocation:fail system permission denied') {
uni.showModal({
content: '请开启手机定位服务',
showCancel: false
})
} else if (err.errMsg === 'getLocation:fail:system permission denied') {
uni.showModal({
content: '请给微信开启定位权限',
showCancel: false
})
}
}
})
},
over