Geolocation using JavaScript

I have been thinking to buy a mobile which has GPS support. But what a petty now even my laptop supports or helps to find my Geolocation through JavaScript and my browser. Have wondered how !!! Ya me too had same. In this blog will details about how can we work thro’ and its features.

Geolocation Specification:

Actually, W3C have been released API specification of Geolocation from 2008 onwards. And now it become implemented by all the browser. Started from firefox, chrome and now IE9 also supports Geolocation.

The Geolocation API defines a high-level interface to location information associated only with the device hosting the implementation, such as latitude and longitude. The API itself is agnostic of the underlying location information sources. Common sources of location information include Global Positioning System (GPS) and location inferred from network signals such as IP address, RFID, WiFi and Bluetooth MAC addresses, and GSM/CDMA cell IDs, as well as user input. No guarantee is given that the API returns the device’s actual location.[W3C – Definition]

How to use in your page:

You can get the Geolocation object from


For best pratice, check if the geolocation object is null, then the browser doesn’t supports Geolocation.




latitude = position.coords.latitude;




  • Requesting repeated position, helps to update the location automatically.
  • Requesting from cached position


Due to privacy issues of retrieving the users location,

W3C has requested a privacy considerations for implementors of Geolocation API (i.e browsers) that, User Agents must not send location Information to Web Sites without permission of user.

So browser have implemented in such a way, Browsers will popup a dialog bar at top, where we can either “allow” or “deny” the service to send to the web site.

Privacy considerations for receipients (i.e. Web Sites which use navigator API),  If the Geolocation information is stored, then it should be allowed to update or delete to user.

Implementation and more properties:

This implementation is for those who gonna implement there own geolocation algorithm,

There are two main Interfaces NavigatorGeolocation and Geolocation.

Geolocation interfaces has signature for all the methods.

Position interface a property.

Coordinates – which holds all the location information. Properties in Coordinates are

  1. latitude
  2. longitude
  3. altitude – height of the position in meters, if implementor cannot provide will be NULL.
  4. accuracy – accuracy level of location in meters.
  5. altitudeAccuracy – both the accuracy should be 95% confidence level.
  6. heading – denotes the direction of travel in degrees,
  7. speed – denotes current ground speed, in m/s.


Most of the commerical sites are started using in their sites.

  • Mostly on Maps, to show up our current position.
  • Apple trailers – get your current position and display the movies shows in your city.

So use the HTML5 – Geolocation and provide better RIA to your clients.

But then when I’m tried to get the location thro’ some test sites, It have been shows current city, location is nearly 20 Kms around.

Smile with tongue out

— The above information are taken from W3C specifications.