轉:https://blog.csdn.net/xiaoxia188/article/details/84703576
getCurrentPosition(successCallback, errorCallback,PositionOptions);
successCallback: 表示獲取到的用戶數據位置。
該對象包含三個屬性:coords、address和timestamp。
coords屬性包含七個值,accuracy:精確度,latitude:緯度,longitude:經度,altitude:海拔,altitudeAcuracy:海拔高度的精確度,heading:朝向,speed:速度。
address屬性包含八個值,country:國家,province:省份,city:城市,district:區/縣,street:路,streetNum:路編號,poiName:地點名稱,cityCode:城市代碼
errorCallback: 返回的錯誤代碼。
包含兩個屬性:message:錯誤信息,code:錯誤代碼
錯誤代碼包含四個值:
unknow_error: 表示不包括在其他錯誤代碼中的錯誤,可以在message中查找信息。
permission_denied: 表示用戶拒絕瀏覽器獲取位置信息的請求。
position unavalablf: 表示網絡不可用或者連接不到衛星。
timeout: 表示獲取超時時。必須在options中指定了timeout值時才有可能發生這種錯誤。
PositionOptions: 數據格式為json,有3個屬性
enableHighAcuracy:布爾值,表示是否啟用高精確度模式,如果啟用這個模式,瀏覽器在獲取位置信息時可能需要耗費更多的時間。
Timeout: 整數,表示瀏覽器需要在指定的時間內獲取位置信息,否則觸發errorCallback。
maximumAge: 整數/常量,表示瀏覽器重新獲取位置信息的時間間隔。
<script>
var x=document.getElementById("demo");
function getLocation() {
if (navigator.geolocation){ //檢測是否支持地理定位
navigator.geolocation.getCurrentPosition(showPosition);
} else{
x.innerHTML="Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
x.innerHTML="Latitude: " + position.coords.latitude +
"<br />Longitude: " + position.coords.longitude;
}
</script>