web網站如何獲取用戶的地理位置
一、總結
一句話總結:通過gps知道用戶的經度和緯度,然后通過經度和緯度在在地圖(google或者百度)上面顯示位置。
1、html5如何通過gps知道用戶的經度和緯度?
通過navigator對象的geolocation屬性的getCurrentPosition方法,在前一個參數showPosition方法中可以獲取經度和緯度
17 navigator.geolocation.getCurrentPosition(showPosition,showError);
2、html5如何通過經度和緯度在在地圖(google或者百度)上面顯示位置?
通過掉用地圖插件(比如)的api,先引入js,然后調用API
10 <script src="http://maps.google.com/maps/api/js?sensor=false"></script>
31 var myOptions={ 32 center:latlon,zoom:14, 33 mapTypeId:google.maps.MapTypeId.ROADMAP, 34 mapTypeControl:false, 35 navigationControlOptions:{style:google.maps.NavigationControlStyle.SMALL} 36 }; 37 var map=new google.maps.Map(document.getElementById("mapholder"),myOptions); 38 var marker=new google.maps.Marker({position:latlon,map:map,title:"You are here!"}); 39 }
3、html5如何通過經度和緯度在在地圖(google或者百度)上面顯示位置(強調經度和緯度數據)?
將獲取的經度和緯度放在google或者百度地圖API的數據位置,latlon是經度和緯度數據
google兩種方式
38 var marker=new google.maps.Marker({position:latlon,map:map,title:"You are here!"});
var img_url="http://maps.googleapis.com/maps/api/staticmap?center=" +latlon+"&zoom=14&size=400x300&sensor=false";
二、web網站如何獲取用戶的地理位置
1、代碼實例
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> 5 </head> 6 <body> 7 <p id="demo">點擊這個按鈕,獲得您的位置:</p> 8 <button onclick="getLocation()">試一下</button> 9 <div id="mapholder"></div> 10 <script src="http://maps.google.com/maps/api/js?sensor=false"></script> 11 <script> 12 var x=document.getElementById("demo"); 13 function getLocation() 14 { 15 if (navigator.geolocation) 16 { 17 navigator.geolocation.getCurrentPosition(showPosition,showError); 18 } 19 else{x.innerHTML="Geolocation is not supported by this browser.";} 20 } 21 22 function showPosition(position) 23 { 24 lat=position.coords.latitude; 25 lon=position.coords.longitude; 26 latlon=new google.maps.LatLng(lat, lon) 27 mapholder=document.getElementById('mapholder') 28 mapholder.style.height='500px'; 29 mapholder.style.width='100%'; 30 31 var myOptions={ 32 center:latlon,zoom:14, 33 mapTypeId:google.maps.MapTypeId.ROADMAP, 34 mapTypeControl:false, 35 navigationControlOptions:{style:google.maps.NavigationControlStyle.SMALL} 36 }; 37 var map=new google.maps.Map(document.getElementById("mapholder"),myOptions); 38 var marker=new google.maps.Marker({position:latlon,map:map,title:"You are here!"}); 39 } 40 41 function showError(error) 42 { 43 switch(error.code) 44 { 45 case error.PERMISSION_DENIED: 46 x.innerHTML="User denied the request for Geolocation." 47 break; 48 case error.POSITION_UNAVAILABLE: 49 x.innerHTML="Location information is unavailable." 50 break; 51 case error.TIMEOUT: 52 x.innerHTML="The request to get user location timed out." 53 break; 54 case error.UNKNOWN_ERROR: 55 x.innerHTML="An unknown error occurred." 56 break; 57 } 58 } 59 </script> 60 </body> 61 </html>
HTML5 Geolocation(地理定位)用於定位用戶的位置。
定位用戶的位置
HTML5 Geolocation API 用於獲得用戶的地理位置。
鑒於該特性可能侵犯用戶的隱私,除非用戶同意,否則用戶位置信息是不可用的。
瀏覽器支持
Internet Explorer 9、Firefox、Chrome、Safari 以及 Opera 支持地理定位。
注釋:對於擁有 GPS 的設備,比如 iPhone,地理定位更加精確。
HTML5 - 使用地理定位
請使用 getCurrentPosition() 方法來獲得用戶的位置。
下例是一個簡單的地理定位實例,可返回用戶位置的經度和緯度。
實例
<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()運行成功,則向參數showPosition中規定的函數返回一個coordinates對象
- showPosition() 函數獲得並顯示經度和緯度
上面的例子是一個非常基礎的地理定位腳本,不含錯誤處理。
處理錯誤和拒絕
getCurrentPosition() 方法的第二個參數用於處理錯誤。它規定當獲取用戶位置失敗時運行的函數:
實例
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 - 無法獲取當前位置
- Timeout - 操作超時
在地圖中顯示結果
如需在地圖中顯示結果,您需要訪問可使用經緯度的地圖服務,比如谷歌地圖或百度地圖:
實例
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+"' />"; }
在上例中,我們使用返回的經緯度數據在谷歌地圖中顯示位置(使用靜態圖像)。
上面的鏈接向您演示如何使用腳本來顯示帶有標記、縮放和拖曳選項的交互式地圖。
給定位置的信息
本頁演示的是如何在地圖上顯示用戶的位置。不過,地理定位對於給定位置的信息同樣很有用處。
案例:
- 更新本地信息
- 顯示用戶周圍的興趣點
- 交互式車載導航系統 (GPS)
getCurrentPosition() 方法 - 返回數據
若成功,則 getCurrentPosition() 方法返回對象。始終會返回 latitude、longitude 以及 accuracy 屬性。如果可用,則會返回其他下面的屬性。
屬性 | 描述 |
---|---|
coords.latitude | 十進制數的緯度 |
coords.longitude | 十進制數的經度 |
coords.accuracy | 位置精度 |
coords.altitude | 海拔,海平面以上以米計 |
coords.altitudeAccuracy | 位置的海拔精度 |
coords.heading | 方向,從正北開始以度計 |
coords.speed | 速度,以米/每秒計 |
timestamp | 響應的日期/時間 |
Geolocation 對象 - 其他有趣的方法
watchPosition() - 返回用戶的當前位置,並繼續返回用戶移動時的更新位置(就像汽車上的 GPS)。
clearWatch() - 停止 watchPosition() 方法
下面的例子展示 watchPosition() 方法。您需要一台精確的 GPS 設備來測試該例(比如 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>