Get geo location data using HTML5 & JavaScript

Get geo location data using HTML5 & JavaScript

In case you are using Google Chrome you will have to enable getting location by the following steps:

  • Open Google Chrome > on the top right click on the vertical 3 dots then select Settings
  • At the bottom click Show advanced settings
  • In the “Privacy” section, click Content settings
  • In the dialog that appears, scroll down to the “Location” section. Select your default permission for future location requests:
    • Allow all sites to track your physical location: Select this option to let all sites automatically see your location.
    • Ask when a site tries to track your physical location: Select this option if you want Google Chrome to alert you whenever a site wants to see your location.
    • Do not allow any site to track your physical location: Select this option to automatically deny requests for your location.
google chrome settings
google chrome content settings

Here is the source code that you can use to get the Geo location:

 var x = document.getElementById("myLoc"); // MyLoc is any html placeholder that will show the location data
        function getLocation(){
            if (navigator.geolocation) {
                navigator.geolocation.watchPosition(showPosition);
            }
            else {
                x.innerHTML = "Geo location is not supported for this broswer.";
            }
        }

        function showPosition(position){
            x.innerHTML = "Latitude: " + position.coords.latitude + "
 Longitude: " + position.coords.longitude;
        }

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *