参考文章:https://blog.csdn.net/qq_42143163/article/details/103681740
uni.getLocation({
type: 'wgs84',
success: ret => {
console.log(ret);
uni.hideLoading();
if(ret.address.poiName){
that.address = ret.address.poiName;
}else{
let latitude = ret.latitude
let longitude = ret.longitude
var point = new plus.maps.Point(ret.longitude, ret.latitude);
plus.maps.Map.reverseGeocode(
point, {},
function(event) {
console.log(event);
var address = event.address; // 转换后的地理位置
_this.address = address;
var point = event.coord; // 转换后的坐标信息
var coordType = event.coordType; // 转换后的坐标系类型
console.log(address, 'address');
var reg = /.+?(省|市|自治区|自治州|县|区)/g;
console.log(address.match(reg));
let addressList = address.match(reg).toString().split(",");
console.log(addressList[0]);
console.log(addressList[1]);
console.log(addressList[2]);
}
);
}
},
fail: err => {
uni.getSystemInfo({
success(res) {
if (res.platform == 'ios') { //IOS
var cllocationManger = plus.ios.import("CLLocationManager");
var enable = cllocationManger.locationServicesEnabled();
var status = cllocationManger.authorizationStatus();
plus.ios.deleteObject(cllocationManger);
if (enable && status == 2) {
uni.showModal({
title: '提示',
content: '请打开定位服务功能',
success(com) {
if (com.confirm == true) {
plus.runtime.openURL("app-settings://");
}
}
})
} else {
uni.showModal({
title: '提示',
content: '未定位到当前城市,请选择城市',
showCancel: false,
success(com) {
that.showdAddress = true
}
})
}
} else if (res.platform == 'android') { //安卓
var context = plus.android.importClass("android.content.Context");
var locationManager = plus.android.importClass("android.location.LocationManager");
var main = plus.android.runtimeMainActivity();
var mainSvr = main.getSystemService(context.LOCATION_SERVICE);
if (!mainSvr.isProviderEnabled(locationManager.GPS_PROVIDER)) {
uni.showModal({
title: '提示',
content: '请打开定位服务功能',
showCancel: false, // 不显示取消按钮
success(com) {
if (com.confirm == true) {
if (!mainSvr.isProviderEnabled(locationManager.GPS_PROVIDER)) {
var Intent = plus.android.importClass('android.content.Intent');
var Settings = plus.android.importClass('android.provider.Settings');
var intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
main.startActivity(intent); // 打开系统设置GPS服务页面
} else {
console.log('GPS功能已开启');
}
}
}
});
} else {
uni.showModal({
title: '提示',
content: '未定位到当前城市,请选择城市',
showCancel: false,
success() {
that.showdAddress = true
}
})
}
}
}
});
}
});