微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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
}

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐