這篇文章主要介紹了HTML5如何使用地理定位,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
成都地區(qū)優(yōu)秀IDC服務(wù)器托管提供商(創(chuàng)新互聯(lián)).為客戶(hù)提供專(zhuān)業(yè)的遂寧托管服務(wù)器,四川各地服務(wù)器托管,遂寧托管服務(wù)器、多線(xiàn)服務(wù)器托管.托管咨詢(xún)專(zhuān)線(xiàn):18980820575
HTML5 Geolocation API 用于獲得用戶(hù)的地理位置。
鑒于該特性可能侵犯用戶(hù)的隱私,除非用戶(hù)同意,否則用戶(hù)位置信息是不可用的。
Internet Explorer 9、Firefox、Chrome、Safari 以及 Opera 支持地理定位。
注釋?zhuān)簩?duì)于擁有 GPS 的設(shè)備,比如 iPhone,地理定位更加精確。
請(qǐng)使用 getCurrentPosition() 方法來(lái)獲得用戶(hù)的位置。
下例是一個(gè)簡(jiǎn)單的地理定位實(shí)例,可返回用戶(hù)位置的經(jīng)度和緯度。
<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>
例子解釋?zhuān)?/p>
檢測(cè)是否支持地理定位
如果支持,則運(yùn)行 getCurrentPosition() 方法。如果不支持,則向用戶(hù)顯示一段消息。
如果getCurrentPosition()運(yùn)行成功,則向參數(shù)showPosition中規(guī)定的函數(shù)返回一個(gè)coordinates對(duì)象
showPosition() 函數(shù)獲得并顯示經(jīng)度和緯度
getCurrentPosition() 方法的第二個(gè)參數(shù)用于處理錯(cuò)誤。它規(guī)定當(dāng)獲取用戶(hù)位置失敗時(shí)運(yùn)行的函數(shù):
function showError(error) { switch(error.code) { case error.PERMISSION_DENIED: x.innerHTML="User denied the request for Geolocation." break; case error.POSITION_UNAVAILABLE: x.innerHTML="Location information is unavailable." break; case error.TIMEOUT: x.innerHTML="The request to get user location timed out." break; case error.UNKNOWN_ERROR: x.innerHTML="An unknown error occurred." break; } }
錯(cuò)誤代碼:
Permission denied - 用戶(hù)不允許地理定位
Position unavailable - 無(wú)法獲取當(dāng)前位置
Timeout - 操作超時(shí)
如需在地圖中顯示結(jié)果,您需要訪(fǎng)問(wèn)可使用經(jīng)緯度的地圖服務(wù),比如谷歌地圖或百度地圖:
function showPosition(position) { var latlon=position.coords.latitude+","+position.coords.longitude; var img_url="http://maps.googleapis.com/maps/api/staticmap?center=" +latlon+"&zoom=14&size=400x300&sensor=false"; document.getElementById("mapholder").innerHTML="<img src='"+img_url+"' />"; }
在上例中,我們使用返回的經(jīng)緯度數(shù)據(jù)在谷歌地圖中顯示位置(使用靜態(tài)圖像)。
谷歌地圖腳本實(shí)例
<!DOCTYPE html>
<html>
<body>
<p id="demo">點(diǎn)擊這個(gè)按鈕,獲得您的位置:</p>
<button onclick="getLocation()">試一下</button>
<div id="mapholder"></div>
<script src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script>
var x=document.getElementById("demo");
function getLocation()
{
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(showPosition,showError);
}
else{x.innerHTML="Geolocation is not supported by this browser.";}
}
function showPosition(position)
{
lat=position.coords.latitude;
lon=position.coords.longitude;
latlon=new google.maps.LatLng(lat, lon)
mapholder=document.getElementById('mapholder')
mapholder.style.height='250px';
mapholder.style.width='500px';
var myOptions={
center:latlon,zoom:14,
mapTypeId:google.maps.MapTypeId.ROADMAP,
mapTypeControl:false,
navigationControlOptions:{style:google.maps.NavigationControlStyle.SMALL}
};
var map=new google.maps.Map(document.getElementById("mapholder"),myOptions);
var marker=new google.maps.Marker({position:latlon,map:map,title:"You are here!"});
}
function showError(error)
{
switch(error.code)
{
case error.PERMISSION_DENIED:
x.innerHTML="User denied the request for Geolocation."
break;
case error.POSITION_UNAVAILABLE:
x.innerHTML="Location information is unavailable."
break;
case error.TIMEOUT:
x.innerHTML="The request to get user location timed out."
break;
case error.UNKNOWN_ERROR:
x.innerHTML="An unknown error occurred."
break;
}
}
</script>
</body>
</html>
上面的實(shí)例向您演示如何使用腳本來(lái)顯示帶有標(biāo)記、縮放和拖曳選項(xiàng)的交互式地圖。
案例:
更新本地信息
顯示用戶(hù)周?chē)呐d趣點(diǎn)
交互式車(chē)載導(dǎo)航系統(tǒng) (GPS)
若成功,則 getCurrentPosition() 方法返回對(duì)象。始終會(huì)返回 latitude、longitude 以及 accuracy 屬性。如果可用,則會(huì)返回其他下面的屬性。
watchPosition() - 返回用戶(hù)的當(dāng)前位置,并繼續(xù)返回用戶(hù)移動(dòng)時(shí)的更新位置(就像汽車(chē)上的 GPS)。
clearWatch() - 停止 watchPosition() 方法
下面的例子展示 watchPosition() 方法。您需要一臺(tái)精確的 GPS 設(shè)備來(lái)測(cè)試該例(比如 iPhone)
<script> var x=document.getElementById("demo"); function getLocation() { if (navigator.geolocation) { navigator.geolocation.watchPosition(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>
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“HTML5如何使用地理定位”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!
新聞標(biāo)題:HTML5如何使用地理定位
文章轉(zhuǎn)載:http://sd-ha.com/article42/gegehc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、營(yíng)銷(xiāo)型網(wǎng)站建設(shè)、面包屑導(dǎo)航、手機(jī)網(wǎng)站建設(shè)、網(wǎng)站營(yíng)銷(xiāo)、定制網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)