HTML5地理定位API允許您與喜愛的網站共享您的位置。JavaScript可以捕獲您的緯度和經度,並發送到後端web伺服器,做一些有趣的位置感知的事情,如查找本地企業或在地圖上顯示您的位置。
現在大多數瀏覽器和移動設備都支持地理定位API。geolocationapi使用全局導航器對象的新屬性,即geolocation對象,可以創建如下−
var geolocation = navigator.geolocation;
geolocation對象是一個服務對象,它允許小部件檢索有關設備地理位置的信息。
Geolocation Methods
geolocation對象提供以下方法−
Sr.No. | Method & Description |
---|---|
1 | getCurrentPosition()
此方法檢索用戶的當前地理位置。 |
2 | watchPosition()
此方法檢索有關設備當前地理位置的定期更新。 |
3 | clearWatch()
此方法取消正在進行的監視位置調用。 |
Example
下面是使用上述任何方法的示例代碼−
function getLocation() { var geolocation = navigator.geolocation; geolocation.getCurrentPosition(showLocation, errorHandler); }
這裡showLocation和errorHandler是回調方法,用於獲取下一節中解釋的實際位置,並在有錯誤時處理錯誤。
Location Properties
Geolocation方法getCurrentPosition()和getPositionUsingMethodName()指定檢索位置信息的回調方法。這些方法是用存儲完整位置信息的對象Position異步調用的。
位置對象指定設備的當前地理位置。位置表示爲一組地理坐標以及有關航向和速度的信息。
下表描述了Position對象的屬性。對於可選屬性,如果系統無法提供值,則該屬性的值設置爲空。
Property | Type | Description |
---|---|---|
coords | objects | Specifies the geographic location of the device. The location is expressed as a set of geographic coordinates together with information about heading and speed. |
coords.latitude | Number | Specifies the latitude estimate in decimal degrees. The value range is [-90.00, +90.00]. |
coords.longitude | Number | Specifies the longitude estimate in decimal degrees. The value range is [-180.00, +180.00]. |
coords.altitude | Number | [Optional] Specifies the altitude estimate in meters above the WGS 84 ellipsoid. |
coords.accuracy | Number | [Optional] Specifies the accuracy of the latitude and longitude estimates in meters. |
coords.altitudeAccuracy | Number | [Optional] Specifies the accuracy of the altitude estimate in meters. |
coords.heading | Number | [Optional] Specifies the device's current direction of movement in degrees counting clockwise relative to true north. |
coords.speed | Number | [Optional] Specifies the device's current ground speed in meters per second. |
timestamp | date | Specifies the time when the location information was retrieved and the Position object created. |
Example
下面是一個使用Position對象的示例代碼。這裡showLocation方法是一個回調方法−
function showLocation( position ) { var latitude = position.coords.latitude; var longitude = position.coords.longitude; ... }
Handling Errors
地理定位是一項複雜的工作,它需要捕捉任何錯誤,並對其進行優雅的處理。
geolocations方法getCurrentPosition()和watchPosition()使用一個錯誤處理程序回調方法,該方法提供PositionError對象。這個對象有以下兩個屬性&負;
Property | Type | Description |
---|---|---|
code | Number | Contains a numeric code for the error. |
message | String | Contains a human-readable description of the error. |
下表描述了PositionError對象中返回的可能錯誤代碼。
Code | Constant | Description |
---|---|---|
0 | UNKNOWN_ERROR | The method failed to retrieve the location of the device due to an unknown error. |
1 | PERMISSION_DENIED | The method failed to retrieve the location of the device because the application does not have permission to use the Location Service. |
2 | POSITION_UNAVAILABLE | The location of the device could not be determined. |
3 | TIMEOUT | The method was unable to retrieve the location information within the specified maximum timeout interval. |
Example
下面是使用PositionError對象的示例代碼。這裡的errorHandler方法是一個回調方法−
function errorHandler( err ) { if (err.code == 1) { // access is denied } ... }
Position Options
下面是getCurrentPosition()方法的實際語法−
getCurrentPosition(callback, ErrorCallback, options)
這裡的第三個參數是PositionOptions對象,它指定一組用於檢索設備地理位置的選項。
下面是可以指定爲第三個參數的選項−
Property | Type | Description |
---|---|---|
enableHighAccuracy | Boolean | Specifies whether the widget wants to receive the most accurate location estimate possible. By default this is false. |
timeout | Number | The timeout property is the number of milliseconds your web application is willing to wait for a position. |
maximumAge | Number | Specifies the expiry time in milliseconds for cached location information. |
Example
下面是一個示例代碼,演示如何使用上述方法−