Custom Web, Mobile App and Software Development Company- ST Solutions.

HTML5 Geolocation API – Specification and Usages

by Ashraful Islam//in HTML 5

The Geolocation API used to share current location with trusted apps and websites to find local businesses or showing current position on a map. The latitude and longitude are available to JavaScript after accepting the permission from the user and send it back to the remote web server. The API acquires the actual location, it is not guaranteed and it itself is agnostic of the underlying location information sources include GPS (Global Positioning System) and location inferred from network signals such as IP address, WiFi, RFID, Bluetooth MAC addresses, GSM/CDMA cell IDs, as well as user input.

Why or where GeoLocation API can be used?

The API can be enabled for both “one-shot” position requests and continuous position updates, as well as the ability to sincerely query for the cached locations. The API allows users to provide their current location to apps or website, if they so desire. For privacy reasons, the user is prompted for permission to share location information.

Want more!! Following possible ideas will help to figure out where this feature can be used:

  1. Use it to guide someone to a destination point like a concert or your local business etc.
  2. It could also be used for “Geo-tagging” some content the user has created.
  3. The user can mark where a photo was taken.(Facebook, Instagram, Google+ has similar feature)
  4. Calculate location’s distance when you are on travel.
  5. Track movement of something by using its current location

and more.

The API doesn’t care about location determination of the browser, because it is device-agnostic. So long as visitors can request and receive location data in a standard way and mechanism might be via WiFi, GPS or simply asking to enter the user location manually. All these lookups take some time to process the request. But the API lets you to pass request with a callback method as the API is asynchronous.

Browser Compatibility

The W3C Geolocation API is supported currently by the following desktop browsers:

  • Mozilla Firefox 3.5+
  • Google Chrome 5.0+
  • Safari 5.0+
  • Opera 10.60+
  • Internet Explorer 9.0+

Some mobile devices are also support the W3C Geolocation API:

  • Android 2.0+
  • iPhone 3.0+
  • Opera Mobile 10.1+
  • Symbian (S60 3rd & 5th generation)
  • Blackberry OS 6
  • Maemo

more details:

Don’t believe it! You can test for the presence of geolocation thusly by “geolocation” in navigator or navigator.geolocation object:


console.log(‘geolocation is available’);


console.log(‘geolocation IS NOT available’);


Bug Warning:

Mozilla Developer Network said, (“geolocation” in navigator) always returned true even if the API was disabled on Firefox 24 and older versions. This has been fixed with Firefox 25 to comply with the spec. (check following bug 884921).

Specification: Functions and Attributes

In GeoLocation API, two functions are available to obtain a user’s location:

getCurrentPosition and watchPosition

Both of these methods after called return immediately, and then asynchronously push to obtain the current location of a user or device. They can take one, two or three parameters, two of which are optional:

  1. successCallback – called if the method returns successfully

  2. [errorCallback] – called if the method attempt to obtain geographic position fails.

Now lets find the differences of them:

The main difference between getCurrentPosition and watchPosition is that watchPosition keeps informing user’s position code on change, so basically it updates the user’s position. This is very useful if the app is tracking a user or device position when it moves, whereas getCurrentPosition is a once off. Another difference is watchPosition method also returns a watchID which is required to stop the position constantly being updated by calling clearWatch method.

Another method needs to be mentioned because of it’s importance:


The clearWatch() method takes only one argument, the watchID and this value is must first checked, when the method is called. This method is to clear the watchID which is returned by watchPosition of the watch process.

When the user’s position is successfully returned, a number of properties are available within a Position object:

  1. coords
    • Type: object
    • Details: Specifies the geographic coordinates together with information about heading and speed of device location.
  2. coords.latitude
    • Type: double
    • Details: Specifies the latitude value in decimal degrees and the value range is [-90.00, +90.00].
  3. coords.longitude
    • Type: double
    • Details: Specifies the longitude value in decimal degrees and the value range is [-180.00, +180.00].
  4. coords.accuracy
    • Type: double
    • Details: [Optional] Specifies the accuracy of the latitude/longitude estimates in meters.
  5. coords.altitudeAccuracy
    • Type: double or NULL
    • Details: [Optional] Specifies the accuracy of the altitude value in meters.
  6. coords.heading
    • Type: double or NULL
    • Details: [Optional] Specifies values in degrees of the device’s current direction of movement counting clockwise from true north.
  7. coords.speed
    • Type: double or NULL
    • Details: [Optional] Specifies the current ground speed in meters per second of the hosting device.
  8. timestamp
    • Type: DOMTimeStamp
    • Details: Specifies the time in timestamp when the location information was acquired and the Position object available.

You can check more about the Position object and the coordinates interface on the W3 specification itself.


    Ashraful Islam

    CTO at

    Problem Solver. Git Chef. Devoted Web Practitioner. Evil Analyst. Algorithm Freak. Food fanatic.

    Latest posts by Ashraful Islam (see all)

    • Beginning with Node.js and MongoDB – Part 1

    Tags: , ,