HTML5 Geolocation


HTML5 Geolocation is used to locate the user's location.


Locate the user

HTML5 Geolocation API is used to obtain the user's geographic location.

In view of this feature may infringe the user's privacy, unless the user agrees, otherwise the user's location information is not available.


Browser support

Internet Explorer Firefox Opera Google Chrome Safari

Internet Explorer 9+, Firefox, Chrome, Safari and Opera support Geolocation.

Note: Geolocation is more accurate for devices with GPS, such as iPhone.


HTML5 - Use Geolocation

Please use the getCurrentPosition() method to get the user's position.

The following example is a simple geolocation example that returns the longitude and latitude of the user's location:

Example

var x=document.getElementById("demo");
function getLocation()
{
     if (navigator.geolocation)
     {
         navigator.geolocation.getCurrentPosition(showPosition);
     }
     else
     {
         x.innerHTML="This browser does not support obtaining geographic location.";
     }
}
 
function showPosition(position)
{
     x.innerHTML="Latitude: "+ position.coords.latitude + "<br>Longitude: "+ position.coords.longitude;
}

 Try It! 

Example analysis:

  • Check whether geolocation is supported

  • If supported, run the getCurrentPosition() method. If it is not supported, a message is displayed to the user.

  • If getCurrentPosition() runs successfully, it returns a coordinates object to the function specified in the parameter showPosition

  • The showPosition() function obtains and displays the longitude and latitude

The above example is a very basic geolocation script without error handling.

Tip: The location information source of geolocation can include GPS, IP address, RFID, WIFI and Bluetooth MAC address, and GSM/CDMS ID, etc.


Handling Errors and Rejections

The second parameter of the getCurrentPosition() method is used to handle errors. It specifies the function to run when the user location fails:

Example

function showError(error)
{
     switch(error.code)
     {
         case error.PERMISSION_DENIED:
             x.innerHTML="The user rejected the request to obtain the geographic location."
             break;
         case error.POSITION_UNAVAILABLE:
             x.innerHTML="Location information is not available."
             break;
         case error.TIMEOUT:
             x.innerHTML="The request for the user's geographic location has timed out."
             break;
         case error.UNKNOWN_ERROR:
             x.innerHTML="Unknown error."
             break;
     }
}

 Try It! 

Error code:

  • Permission denied: user is not allowed to geolocation

  • Position unavailable: The current position cannot be obtained

  • Timeout: Operation timeout


Show results on the map

To display the results on the map, you need to access a map service that can use latitude and longitude, such as Google Maps or Bing Maps:

Example

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+"'>";
}

 Try It! 

In the above example, we use the returned latitude and longitude data to display the location in Google Maps (using a static image).

Google Maps Script
The link above shows you how to use a script to display an interactive map with markers, zoom, and drag options.


Information for a given location

This page demonstrates how to display the user’s location on the map. However, geolocation is also useful for information about a given location.

can be use on:

  • Update local information

  • Show points of interest around the user

  • Interactive car navigation system (GPS)


getCurrentPosition() method-return data

If T succeeds, the getCurrentPosition() method returns the object. The latitude, longitude, and accuracy properties are always returned. If available, the other attributes below will be returned.

AttributeDescription
coords.latitudeDecimal latitude
coords.longitudeThe longitude of a decimal number
coords.accuracyPosition accuracy
coords.altitudeAltitude, measured in meters above sea level
coords.altitudeAccuracyThe altitude accuracy of the location
coords.headingDirection, measured in degrees from true north
coords.speedSpeed in meters per second
timestampDate/time of response



Geolocation object - other interesting methods

watchPosition(): Returns the user's current location, and continues to return the updated location when the user is moving (just like GPS in a car).

clearWatch(): stop watchPosition() method

The following example shows the watchPosition() method. You need an accurate GPS device to test this example (for example, iPhone):

Example

var x = document.getElementById("demo");

function getLocation()
{
     if (navigator.geolocation)
     {
         navigator.geolocation.watchPosition(showPosition);
     }
     else
     {
         x.innerHTML = "This browser does not support obtaining geographic location.";
     }
}

function showPosition(position)
{
     x.innerHTML = "Latitude: "+ position.coords.latitude + "<br>Longitude: "+ position.coords.longitude;
}

Try It!