1、uniapp要调用腾讯地图,首先要去
腾讯位置服务
上去创建应用绑定
添加key与你小程序appId绑定
2、登录
微信公众平台
,完善小程序信息
3、开通位置权限(这个很重要)(wx.getLocation这个权限不好过审,可能需要多试几次)
4、在uniapp的manifest.json配置清单上配置appId,并选中小程序位置接口,然后打开源码视图,加上你在微信公众平台(上一步)开通的接口权限(这个很重要,不然在微信上没法调用位置)
"mp-weixin" : {
/* 小程序特有相关 */
"appid" : "wxxxxxxxx",
"setting" : {
"urlCheck" : false,
"es6" : true,
"minified" : true,
"postcss" : false
},
"usingComponents" : true,
"permission" : {
"scope.userLocation" : {
"desc" : "你的位置信息将用于小程序位置接口的效果展示"
}
},
"requiredPrivateInfos" : [ "chooseAddress", "choosePoi", "getLocation" ]
},
5、接下来就可以看着uniapp官方文档调用地理位置接口啦!!!
// 获取定位信息
Mixin_GetLocation() {
const that = this
return new Promise((resolve, reject) => {
this.mixin_JudgeWeixinAuthorizeLocation(() => {
uni.getLocation({
type: 'gcj02',
success(response) {
that.myLocation = response
resolve(response)
},
fail() {
reject()
}
})
})
})
},
/**
* 判断小程序 和 APP 有没有定位权限
* @param {function} callback
* @returns {AxiosPromise}
*/
async mixin_JudgeAuthorizeLocation(callback) {
const that = this
// #ifdef MP-WEIXIN
uni.authorize({
scope: 'scope.userLocation',
success(scope) {
if (scope.errMsg === 'authorize:ok') {
callback && callback()
}
},
fail(error) {
console.log('微信小程序获取位置信息失败:', error)
uni.showModal({
title: '提示',
content: '为保证功能正常使用,请开启定位权限',
confirmText: '去开启',
confirmColor: '#426BEB',
success({
confirm
}) {
if (confirm) {
uni.openSetting({
success(res) {
if (res.authSetting['scope.userLocation']) {
that.mixin_JudgeWeixinAuthorizeLocation(
callback)
} else {
that.mixin_JudgeWeixinAuthorizeLocation(
callback)
}
}
})
}
}
})
}
})
// #endif
// #ifdef APP-PLUS
if (plus.os.name === 'Android') {
console.log('APP 端:', 'Android')
const androidLocationAuth = await Permission.requestAndroidPermission(
'android.permission.ACCESS_FINE_LOCATION')
if (androidLocationAuth == 1) { // 已获取授权
callback && callback()
} else if (androidLocationAuth == 0) { // 未获取授权
uni.showModal({
title: '无法访问位置',
content: '您已拒绝访问位置信息,功能无法正常使用,是否重新授权?',
confirmColor: '#426BEB',
confirmText: '重新授权',
success(res) {
if (res.confirm) {
that.mixin_JudgeWeixinAuthorizeLocation()
}
}
})
} else { // 被永久拒绝授权
uni.showModal({
title: '无法访问位置',
content: '当前无定位权限,建议前往系统设置,允许应用访问位置信息',
confirmColor: '#426BEB',
confirmText: '前往系统设置',
success(res) {
if (res.confirm) {
Permission.gotoAppPermissionSetting()
}
}
})
}
} else if (plus.os.name === 'iOS') {
console.log('APP 端:', 'iOS')
if (uni.getStorageSync(Keys.getLocationPermissionNotForFirstTimeByIos)) {
const iosLocationAuth = Permission.judgeIosPermission('location')
if (iosLocationAuth) {
callback && callback()
} else {
uni.showModal({
title: '无法访问位置',
content: '当前无位置访问权限,建议前往系统设置,允许应用访问位置信息',
confirmColor: '#426BEB',
confirmText: '前往系统设置',
success(res) {
if (res.confirm) {
Permission.gotoAppPermissionSetting()
}
}
})
}
} else {
uni.setStorageSync(Keys.getLocationPermissionNotForFirstTimeByIos, true)
callback && callback()
}
}
// #endif
// #ifdef H5
callback && callback()
// #endif
},
const Permission = {
// Android权限查询
requestAndroidPermission(permissionID) {
return new Promise((resolve, reject) => {
plus.android.requestPermissions(
[permissionID],
function(resultObj) {
var result = 0;
for (var i = 0; i < resultObj.granted.length; i++) {
var grantedPermission = resultObj.granted[i];
console.log('已获取的权限:' + grantedPermission);
result = 1
}
for (var i = 0; i < resultObj.deniedPresent.length; i++) {
var deniedPresentPermission = resultObj.deniedPresent[i];
console.log('拒绝本次申请的权限:' + deniedPresentPermission);
result = 0
}
for (var i = 0; i < resultObj.deniedAlways.length; i++) {
var deniedAlwaysPermission = resultObj.deniedAlways[i];
console.log('永久拒绝申请的权限:' + deniedAlwaysPermission);
result = -1
}
resolve(result);
// 若所需权限被拒绝,则打开APP设置界面,可以在APP设置界面打开相应权限
// if (result != 1) {
// gotoAppPermissionSetting()
// }
},
function(error) {
console.log('申请权限错误:' + error.code + " = " + error.message);
resolve({
code: error.code,
message: error.message
});
}
);
});
},
// 使用一个方法,根据参数判断权限
judgeIosPermission(permissionID) {
if (permissionID == "location") {
return judgeIosPermissionLocation()
} else if (permissionID == "camera") {
return judgeIosPermissionCamera()
} else if (permissionID == "photoLibrary") {
return judgeIosPermissionPhotoLibrary()
} else if (permissionID == "record") {
return judgeIosPermissionRecord()
} else if (permissionID == "push") {
return judgeIosPermissionPush()
} else if (permissionID == "contact") {
return judgeIosPermissionContact()
} else if (permissionID == "calendar") {
return judgeIosPermissionCalendar()
} else if (permissionID == "memo") {
return judgeIosPermissionMemo()
}
return false;
},
// 跳转到**应用**的权限页面
gotoAppPermissionSetting() {
if (isIos) {
var UIApplication = plus.ios.import("UIApplication");
var application2 = UIApplication.sharedApplication();
var NSURL2 = plus.ios.import("NSURL");
// var setting2 = NSURL2.URLWithString("prefs:root=LOCATION_SERVICES");
var setting2 = NSURL2.URLWithString("app-settings:");
application2.openURL(setting2);
plus.ios.deleteObject(setting2);
plus.ios.deleteObject(NSURL2);
plus.ios.deleteObject(application2);
} else {
var Intent = plus.android.importClass("android.content.Intent");
var Settings = plus.android.importClass("android.provider.Settings");
var Uri = plus.android.importClass("android.net.Uri");
var mainActivity = plus.android.runtimeMainActivity();
var intent = new Intent();
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
var uri = Uri.fromParts("package", mainActivity.getPackageName(), null);
intent.setData(uri);
mainActivity.startActivity(intent);
}
},
}
版权声明:本文为qq_44761755原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。