HTML5如何使用地理定位

這篇文章主要介紹了HTML5如何使用地理定位,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

成都地區(qū)優(yōu)秀IDC服務(wù)器托管提供商(創(chuàng)新互聯(lián)).為客戶提供專業(yè)的遂寧托管服務(wù)器,四川各地服務(wù)器托管,遂寧托管服務(wù)器、多線服務(wù)器托管.托管咨詢專線:18980820575

定位用戶的位置

HTML5 Geolocation API 用于獲得用戶的地理位置。

鑒于該特性可能侵犯用戶的隱私,除非用戶同意,否則用戶位置信息是不可用的。

瀏覽器支持

Internet Explorer 9、Firefox、Chrome、Safari 以及 Opera 支持地理定位。

注釋:對于擁有 GPS 的設(shè)備,比如 iPhone,地理定位更加精確。

HTML5 - 

請使用 getCurrentPosition() 方法來獲得用戶的位置。

下例是一個簡單的地理定位實例,可返回用戶位置的經(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>

例子解釋:

  • 檢測是否支持地理定位

  • 如果支持,則運行 getCurrentPosition() 方法。如果不支持,則向用戶顯示一段消息。

  • 如果getCurrentPosition()運行成功,則向參數(shù)showPosition中規(guī)定的函數(shù)返回一個coordinates對象

  • showPosition() 函數(shù)獲得并顯示經(jīng)度和緯度

處理錯誤和拒絕

getCurrentPosition() 方法的第二個參數(shù)用于處理錯誤。它規(guī)定當(dāng)獲取用戶位置失敗時運行的函數(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;
  }
 }

錯誤代碼:

  • Permission denied - 用戶不允許地理定位

  • Position unavailable - 無法獲取當(dāng)前位置

  • Timeout - 操作超時

在地圖中顯示結(jié)果

如需在地圖中顯示結(jié)果,您需要訪問可使用經(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)圖像)。

谷歌地圖腳本實例

<!DOCTYPE html>
<html>
<body>
<p id="demo">點擊這個按鈕,獲得您的位置:</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>

上面的實例向您演示如何使用腳本來顯示帶有標(biāo)記、縮放和拖曳選項的交互式地圖。

給定位置的信息

案例:

  • 更新本地信息

  • 顯示用戶周圍的興趣點

  • 交互式車載導(dǎo)航系統(tǒng) (GPS)

getCurrentPosition() 方法 - 返回數(shù)據(jù)

若成功,則 getCurrentPosition() 方法返回對象。始終會返回 latitude、longitude 以及 accuracy 屬性。如果可用,則會返回其他下面的屬性。

  • HTML5如何使用地理定位

  • Geolocation 對象 - 其他有趣的方法

    watchPosition() - 返回用戶的當(dāng)前位置,并繼續(xù)返回用戶移動時的更新位置(就像汽車上的 GPS)。

    clearWatch() - 停止 watchPosition() 方法

    下面的例子展示 watchPosition() 方法。您需要一臺精確的 GPS 設(shè)備來測試該例(比如 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如何使用地理定位”這篇文章對大家有幫助,同時也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!

新聞標(biāo)題:HTML5如何使用地理定位
文章轉(zhuǎn)載:http://bm7419.com/article42/gegehc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、營銷型網(wǎng)站建設(shè)、面包屑導(dǎo)航手機(jī)網(wǎng)站建設(shè)、網(wǎng)站營銷定制網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

手機(jī)網(wǎng)站建設(shè)