HBuilder uniapp手机定位
作者:互联网
// 获取当前位置 getPosition: function() { this.GetLocation(function(res) { if (res) { this.longitude = res.longitude;// 经度 this.latitude = res.latitude;//纬度 this.address = res.address;//五级地址 } }) }, GetLocation:function(callBack) {
//#ifdef APP-PLUS
let system = uni.getSystemInfoSync() //获取系统信息
if (system.platform == "android" || 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); var gpsProvider = mainSvr.isProviderEnabled(locationManager.GPS_PROVIDER); //检查是否开启了GPS if (!gpsProvider) { uni.showModal({ title: '提示', content: '请打开手机定位服务权限', showCancel: false, success: res => { if (!gpsProvider) { var Intent = plus.android.importClass("android.content.Intent"); var Settings = plus.android.importClass("android.provider.Settings"); var new_Intent = new Intent(Settings.ACTIVE_LOCATION_SOURCE_SETTINGS); main.startActivity(new_Intent); } }, }); } else { uni.showLoading({ title:'定位中...' }) uni.getLocation({ type: 'wgs84', altitude: false, geocode: true, success: (res) => { uni.hideLoading(); // console.log('定位结果:' + JSON.stringify(res)); var addressList = res.address; var address = addressList.province + '' + addressList.city + '' + addressList.district + '' + addressList.street + '' + addressList.streetNum; console.log(JSON.stringify(res)); var location = { longitude: res.longitude, latitude: res.latitude, address: address, type: res.type }; callBack(location); }, fail: (err) => { //这部分不需要可以删除 uni.hideLoading(); // #ifdef MP-BAIDU if (err.errCode === 202 || err.errCode === 10003) { // 202模拟器 10003真机 user deny // this.showConfirm(); } // #endif // #ifndef MP-BAIDU if (err.errMsg.indexOf("auth deny") >= 0) { uni.showToast({ title: "访问位置被拒绝" }) } else { uni.showToast({ title: err.errMsg }) } // #endif return callBack(err.errMsg); } }) }
}
//#endif
}
标签:uniapp,定位,res,HBuilder,addressList,address,var,uni,android 来源: https://www.cnblogs.com/nifty/p/15194408.html