Get Geolocation in Browser using HTML5

HTML 5 allows obtaining of your website visitor’s Geo location through navigator.geolocation object. To obtain the visitor’s current location, you can call the getCurrentPosition() method. This initiates a request to detect the user’s position. Syntax of getCurrentPosition() method …

HTML 5 allows obtaining of your website visitor’s Geo location through navigator.geolocation object. To obtain the visitor’s current location, you can call the getCurrentPosition() method. This initiates a request to detect the user’s position. Syntax of getCurrentPosition() method is :

navigator.geolocation.getCurrentPosition(success, error, options)

success : A callback function that takes position as input and use it for further processing.

error (optional) : A callback function for error handling.

options (optional) : A postion option object which could be used to define available options.

A simple script to obtain user’s current location can be written as :

 

do_something is your function which further processes visitor’s latitude and longitude . You can save the information in database, display Geo-targetted content or simply display the user’s location on map.
As it is being done below :