Wednesday, 2 September 2015

How to Access User Location - Latitude and Longitude?

You can now take advantage of HTML5 Geolocation API in order to get user's location details such as latitude and longitude.

What was our requirement?
1) Whenever user visits our website, we want to track user's location (e.g. latitude and longitude).  
2) Page should ask to access location only one time during that session, it should not ask every-time.
    Using geolocation API will ask user to whether user wants to share location or not?

Implementation:
Below code snippet shows how to use HTML Geolocation API and store user's location details in cookie so that it should not ask to share location each time.

Step-1:
Define two hidden textboxes to store user's latitude and longitude value.
   
   
Step-2: Write jQuery to access user's latitude and longitude and store it in cookie for future use.

HTML5 Geolocation API browser compatibility:
If you want to know which browsers support HTML5 Geolocation API then below screen-shot can help you.



User will be asked to share location only one time, then it will be stored in cookie for future use.



Hope this may help you to trace your user's location!

No comments:

Post a Comment