# 地理位置 Geolocation API
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(locationSuccess, locationError,{
// 指示瀏覽器獲取高精度的位置,默認(rèn)為false
enableHighAccuracy: true,
// 指定獲取地理位置的超時(shí)時(shí)間剖煌,默認(rèn)不限時(shí)巴元,單位為毫秒
timeout: 50000,
// 最長(zhǎng)有效期兼贸,在重復(fù)獲取地理位置時(shí)击费,此參數(shù)指定多久再次獲取位置铲咨。
maximumAge: 30000
});
}else{
alert("您的瀏覽器不支持地理定位嘲更。");
}
//成功時(shí)執(zhí)行的回調(diào)函數(shù)
function locationSuccess(position){
var coords = position.coords;
console.log(coords);
coords.latitude,// 維度
coords.longitude // 精度
coords.latitude;//十進(jìn)制數(shù)的緯度
coords.longitude;//十進(jìn)制數(shù)的經(jīng)度
coords.accuracy;位置精度
coords.altitude;//海拔闸溃,海平面以上以米計(jì)
coords.altitudeAccuracy;//位置的海拔精度
coords.heading;//方向勋陪,從正北開始以度計(jì)
coords.speed;//速度贪磺,以米/每秒計(jì)
}
//失敗時(shí)執(zhí)行的回調(diào)函數(shù)
function locationError(error){
switch(error.code) {
case error.TIMEOUT:
console.log('超時(shí)了');
break;
case error.POSITION_UNAVAILABLE:
console.log('獲取地理位置失敗!');
break;
case error.PERMISSION_DENIED:
console.log('填寫的地理位置有誤!');
break;
case error.UNKNOWN_ERROR:
console.log('未知錯(cuò)誤!');
break;
}
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者