uniapp 获取当前定位
作者:互联网
需要打开手机的gps开关,正常的授权也要有
判断当前设备是否打开gps开关
checkOpenGPSServiceByAndroid () {
let system = uni.getSystemInfoSync();// 获取系统信息
if (system.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() {
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服务页面
}
}
});
}
}
},
获取当前定位,经纬度转地址
getnow() {
uni.getLocation({
type: 'wgs84',
geocode: true,
success: (res) =>{
let point = new plus.maps.Point(res.longitude, res.latitude);
plus.maps.Map.reverseGeocode(
point, {},
(event) =>{
let address = event.address; // 转换后的地理位置
let point = event.coord; // 转换后的坐标信息
let coordType = event.coordType; // 转换后的坐标系类型
let reg = /.+?(省|市|自治区|自治州|县|区)/g;
let addressList = address.match(reg).toString().split(",");
console.log(addressList,'位置信息')
}
)
}
});
},
标签:uniapp,定位,获取,plus,importClass,var,let,android,event 来源: https://www.cnblogs.com/newBugs/p/15817319.html