1

I have written the following code to access my location, but this code is not showing my current location.

What are the changes that I need to make. Because I am a beginner and I don't know much.

Thank you in advance.

<body>
    <a href="#" id="get_location">Get location</a>
    <div id="map">
        <iframe id="google_map" width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="https://maps.google.co.in?output=embed"></iframe> 
    </div>
    <script>
        var c= function(pos)
        {
            var lat= pos.coords.latitude, long= pos.coords.longitude, coords= lat + ',' + long;
            document.getElementById('google_map').setAttribute('src','https://maps.google.co.in/?q=' + coords + '&z=60&output=embed');
        }
        document.getElementById('get_loaction').onclick = function()
        {   
            navigator.geolocation.getCurrentPosition(c);
            return false;
        }
    </script>
</body>
Sarvan Kumar
  • 926
  • 1
  • 11
  • 27

1 Answers1

0

First, you need to check if browser support Geolocation :

// check for Geolocation support
if (navigator.geolocation) {
   console.log('Geolocation is supported!');
     navigator.geolocation.getCurrentPosition(function(position) {

        // Get the coordinates of the current possition.
        // here u got your location Latitude & Longitude
        var lat = position.coords.latitude;
        var lng = position.coords.longitude;
     }
}
else {
   console.log('Geolocation is not supported for this Browser/OS.');
}

There is a really good example here: https://jsfiddle.net/ubrvm4ao/672/

Emad Dehnavi
  • 3,262
  • 4
  • 19
  • 44