Find latitude and longitude of current location
Asked Answered
R

3

6

I am using this code for finding current location latitude and longitude.

$(document).ready(function() {
    if (navigator.geolocation)
    {
        navigator.geolocation.getCurrentPosition(successFunction, errorFunction);
    }
    else 
    {
        alert('It seems like Geolocation, which is required for this page, is not enabled in your browser.');
    }       
});

function successFunction(position) 
{
    var lat = position.coords.latitude;
    var long = position.coords.longitude;
    alert('Your latitude is :'+lat+' and longitude is '+long);
}

function errorFunction(position) 
{
    alert('Error!');
}

It's working fine in Chrome but not in Mozilla. There is no alert error message either.

Rupture answered 9/7, 2013 at 5:18 Comment(4)
Check your Firefox developer console (Ctrl+Shift+I, or F12 if Firebug is instaled) for script errors or elseAesthete
does your code ask for permission to use the feature? Any add-on hosted on addons.mozilla.org which makes use of geolocation data must explicitly request permission before doing so from the mdn fopund hereBrahmani
it works for me in both Chrome and Mozilla.Histidine
The problem was very minor, you have defined variable name long check.. var long = its keyword in javascriptMaxillary
H
11

Try this i hope this will help you:

<!DOCTYPE html>
<html>
  <head>
   <script type="text/javascript">
     function initGeolocation()
     {
        if( navigator.geolocation )
        {
           // Call getCurrentPosition with success and failure callbacks
           navigator.geolocation.getCurrentPosition( success, fail );
        }
        else
        {
           alert("Sorry, your browser does not support geolocation services.");
        }
     }

     function success(position)
     {

         document.getElementById('long').value = position.coords.longitude;
         document.getElementById('lat').value = position.coords.latitude
     }

     function fail()
     {
        // Could not obtain location
     }

   </script>    
 </head>

 <body onLoad="initGeolocation();">
   <FORM NAME="rd" METHOD="POST" ACTION="index.html">
     <INPUT TYPE="text" NAME="long" ID="long" VALUE="">
     <INPUT TYPE="text" NAME="lat" ID="lat" VALUE="">
 </body>
</html>
Hightower answered 9/7, 2013 at 6:8 Comment(1)
Read this msdn.microsoft.com/en-us/library/windows/hardware/…Hightower
T
1

You can try this code:-

<!DOCTYPE html>
<html>
<body>

<p>Click the button to get your coordinates.</p>

<button onclick="getLocation()">Try It</button>

<p id="demo"></p>

<script>
var x = document.getElementById("demo");

function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition);
    } else { 
        x.innerHTML = "Geolocation is not supported by this browser.";
    }
}

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

</body>
</html>

Reference

Teteak answered 18/11, 2018 at 12:35 Comment(0)
L
0

By using below jQuery code, You can find your current location latitude and longitude without any API key .Let's Try

 $(document).ready(function(){
     
        // get users lat/long
        
        var getPosition = {
          enableHighAccuracy: false,
          timeout: 9000,
          maximumAge: 0
        };
        
        function success(gotPosition) {
          var uLat = gotPosition.coords.latitude;
          var uLon = gotPosition.coords.longitude;
          console.log(`${uLat}`, `${uLon}`);
        
        };
        
        function error(err) {
          console.warn(`ERROR(${err.code}): ${err.message}`);
        };
        
        navigator.geolocation.getCurrentPosition(success, error, getPosition);
        });
Lucretialucretius answered 4/11, 2020 at 10:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.