最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Find latitude and longitude of current location - Stack Overflow

programmeradmin2浏览0评论

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.

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.

Share Improve this question edited Jul 9, 2013 at 5:27 Herbert 5,7783 gold badges27 silver badges34 bronze badges asked Jul 9, 2013 at 5:18 Maneesh MehtaManeesh Mehta 5073 gold badges8 silver badges17 bronze badges 4
  • Check your Firefox developer console (Ctrl+Shift+I, or F12 if Firebug is instaled) for script errors or else – Olegas Commented Jul 9, 2013 at 5:25
  • 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 here – winner_joiner Commented Jul 9, 2013 at 5:27
  • 2 it works for me in both Chrome and Mozilla. – SHANK Commented Jul 9, 2013 at 5:31
  • 1 The problem was very minor, you have defined variable name long check.. var long = its keyword in javascript – Ravi Dhoriya ツ Commented Dec 12, 2014 at 11:11
Add a comment  | 

3 Answers 3

Reset to default 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>

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

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);
        });
发布评论

评论列表(0)

  1. 暂无评论