Location-Aware

The Geolocation application programming interface (API) provides an interface with device location information


The Geolocation API is generic enough to interface with many underlying location information sources such as the Global Positioning System (GPS) and locations inferred from network signals such as IP address, radio frequency identification (RFID), Wi-Fi and Bluetooth MAC addresses, and cellular IDs and from user input.

No guarantee is given that the API returns the device’s actual location, and the accuracy can vary widely

The Geolocation API is not part of HTML5, although support for geolocation is being added to browsers in parallel with HTML5.

The API is designed to enable both one-shot position requests and repeated position updates.

The Geolocation object is accessible by using the navigator.geolocation global variable. The following code snippet determins whether the user's browser provides support.

    function supportsGeolocation() {
        return "geolocation" in navigator;
    }
    

getCurrentPosition()

Use getCurrentPosition() method to get the current position


function GetCurrentLocation() {
    if (SupportsGeolocation()) {
        navigator.geolocation.getCurrentPosition(ShowCurrentPosition);
    } else {
        ShowMessage("Geolocation is not supported by this browser.");
    }
}
    
function ShowCurrentPosition(position) {
    var datetime = new Date(position.timestamp).toLocaleString();
    ShowMessage(dvCurrentPosition, 
        "Latitude: " + position.coords.latitude + "<br />"
      + "Longitude: " + position.coords.longitude + "<br />" 
      + "Timestamp: " + datetime);
} 

watchPosition()

Use watchPosition() method to get continuous position updates