轉:https://blog.csdn.net/qq_38322527/article/details/80758012
plus.geolocation.getCurrentPosition(function(p){
alert('Geolocation\nLatitude:' + p.coords.latitude + '\nLongitude:' + p.coords.longitude + '\nAltitude:' + 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
})