Monday, September 26, 2016

How do I find the current geolocation using JavaScript?

In HTML5 supported browsers you can find your geographical location using the GeoLocation API.

You can access this site for more:

The Navigator object's geolocation should be checked first to verify if it is supported. You can just insert this between script tags.
if (navigator.geolocation){alert("geolocation is supported")};
If Geolocation is supported then this script will write to the browser the latitude and longitude:

var watchID = navigator.geolocation.watchPosition(function(position) {
 document.write( "Longitude= "+position.coords.longitude,"
","Latitude= "+position.coords.latitude);

For the above code in a web page the following response was received. Sometimes there may be browser message asking for permission to use your current location.

My current location is right next to Chaminade University in Honolulu and the coordinates are correct.

No comments: