转:https://blog.csdn.net/qq_38322527/article/details/80758012
plus.geolocation.getCurrentPosition(function(p){
alert('Geolocation
Latitude:' + p.coords.latitude + '
Longitude:' + p.coords.longitude + '
Altitude:' + p.coords.altitude);
console.log('Geolocation info: ' + JSON.stringify(p));
}, function(e){
console.log('Gelocation Error: code - ' + e.code + '; message - ' + e.message);
} );
官方的实例,使用plus.geolocation.getCurrentPosition可以获取手机当前的经纬度信息,且定位相对准确,这里只对失败回调做一个简单说明
plus.geolocation.getCurrentPosition(function(res) {
//成功回调
user_latitude = res.coords.latitude; //纬度
user_longitude = res.coords.longitude; //经度
//mui.alert("经度:"+user_longitude+" 纬度:"+user_latitude);
}, function(e) {
console.log('Gelocation Error: code - ' + e.code + '; message - ' + e.message);
switch(e.code) {
case 10:
mui.alert('请开启应用的定位权限', '温馨提示','确定',function(){},'div');
break;
case 9:
//mui.alert('请开启手机定位服务');
mui.alert('请开启手机定位服务', '温馨提示','确定',function(){},'div');
break;
case 2:
if(e.message.indexOf("[geolocation:13]") > -1) {
//如果网络开启,定位失败,提示检查定位权限
mui.alert('请开启应用的定位权限', '温馨提示','确定',function(){},'div');
}
if(e.message.indexOf("[geolocation:14]") > -1) {
//如果应用的权限开了,提示网络异常
mui.alert('请检查网络是否正常', '温馨提示','确定',function(){},'div');
}
break;
case e.PERMISSION_DENIED:
mui.alert('请求定位被拒绝', '温馨提示','确定',function(){},'div');
break;
case e.POSITION_UNAVAILABLE:
mui.alert("位置信息不可用", '温馨提示','确定',function(){},'div');
break;
case e.TIMEOUT:
mui.alert("获取位置信息超时", '温馨提示','确定',function(){},'div');
break;
case e.UNKNOWN_ERROR:
mui.alert("未知错误", '温馨提示','确定',function(){},'div');
break;
}
}, {
//超时未获取到经纬度信息 执行失败回调 (默认为5秒)
timeout: 3000
})