需要打開手機的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,'位置信息')
}
)
}
});
},