问题:获取手机定位权限,在用户点击拒绝后,再次点击定位按钮,手机无反应。这里,安卓系统2次拒绝之后,默认为禁止询问弹窗弹出,所以再点击定位肯定没有反应。
一、解决思路:第一想到,检查是否打开GPS功能(Android),打开了直接跳转到地图定位页面,关闭状态则跳转系统设置;
以下均为Android示例:
原代码:
import { checkOpenGPSServiceByAndroid } from "@/utils/device";
/*.
.
.
*/
/*
定位按钮触发事件
*/
onGPSAddress() {
checkOpenGPSServiceByAndroid()
// 获取当前位置
uni.getLocation({
type: "gcj02", //返回可以用于uni.openLocation的经纬度
success: res => {
let latitude = res.latitude;
let longitude = res.longitude;
uni.chooseLocation({
latitude,
longitude,
success: data => {
//此处仅返回详细地址和经纬度,所以要进行经纬度解析
this.getCity(data.latitude, data.longitude);
},
fail: fail => {
checkOpenGPSServiceByAndroid();
}
});
}
});
},
// 解析经纬度
getCity(latitude, longitude) {
let location = `${longitude},${latitude}`;
var myAmapFun = new amapFile.AMapWX({
key: amapKey,
batch: true
});
myAmapFun.getRegeo({
//如果经纬度有问题会导致不进入回调方法,从而报错
location: location,
success: e => {
//成功回调
console.log(e);
let addressComponent = e[0].regeocodeData.addressComponent;
let formatted_address = e[0].regeocodeData.formatted_address;
this.detail = formatted_address;
this.city = addressComponent.city;
this.province = addressComponent.province;
this.provinceCity = addressComponent.province + addressComponent.city;
this.district = addressComponent.district;
this.districtId = addressComponent.adcode;
this.location = e[0].location;
this.longitude = e[0].longitude;
},
fail: function (info) {
//失败回调
console.log(info);
}
});
},
device.js文件:
let system = uni.getSystemInfoSync(); // 获取系统信息
/**检查是否打开GPS功能(android)**/
export const checkOpenGPSServiceByAndroid = () => {
if (system.platform === 'android') { // 判断平台
var context = plus.android.importClass("