HTML5 GeoLocation 地理定位

來源:互聯網
上載者:User

標籤:style   http   io   os   ar   使用   for   strong   sp   

window.navigator 對象包含有關訪問者瀏覽器的資訊,window.navigator 對象在編寫時可不使用 window 這個首碼。

瀏覽器支援

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

注釋:對於擁有 GPS 的裝置,比如 iPhone,地理定位更加精確



HTML5 地理定位

html5為window.navigator提供了geolocation屬性,用於擷取基於瀏覽器的目前使用者地理位置。

window.navigator.geolocation提供了3個方法分別是

void getCurrentPosition(onSuccess,onError,options);//擷取使用者當前位置int watchPosition(onSuccess,onError,options);//持續擷取目前使用者位置void clearWatch(watchId);//watchId 為watchCurrentPosition返回的值//取消監控options = {     enableHighAccuracy,//boolean 是否要求高精度的地理資訊     timeout,//擷取資訊的逾時限制     maximumAge//對地理資訊進行緩衝的時間}//options可以不寫,為預設即可


使用getCurrentPosition()來擷取使用者位置(其實擷取的是被使用者使用的瀏覽器的位置):

<!DOCTYPE html>
<html>
<body>
<p id="demo">點擊這個按鈕,獲得您的座標:</p>
<button onclick="getLocation()">試一下</button>
<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){
  x.innerHTML="Latitude: " + position.coords.latitude +
  "<br />Longitude: " + position.coords.longitude;
}
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>


google地圖應用:
和上面不同的是
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+"‘ />";}


下面的例子展示 watchPosition() 方法:

<!DOCTYPE html>
<html>
<body>
<p id="demo">點擊這個按鈕,獲得您的座標:</p>
<button onclick="getLocation()">試一下</button>
<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>
</body>
</html>

 

HTML5 GeoLocation 地理定位

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.