【问题标题】:Find latitude and longitude of current location查找当前位置的经纬度
【发布时间】:2013-07-06 14:27:46
【问题描述】:

我正在使用此代码来查找当前位置的纬度和经度。

$(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!');
}

它在 Chrome 中运行良好,但在 Mozilla 中却不行。也没有警报错误消息。

【问题讨论】:

  • 检查您的 Firefox 开发者控制台(如果安装了 Firebug,则为 Ctrl+Shift+I 或 F12)是否有脚本错误或其他
  • 您的代码是否要求获得使用该功能的权限? Any add-on hosted on addons.mozilla.org which makes use of geolocation data must explicitly request permission before doing so 来自 mdn fopund here
  • 它在 Chrome 和 Mozilla 中都适用于我。
  • 问题很小,你已经定义了变量名long check.. var long = its keyword in javascript

标签: javascript jquery html firefox mozilla


【解决方案1】:

试试这个,希望对你有帮助:

<!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>
【解决方案2】:

你可以试试这个代码:-

<!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

【讨论】:

    【解决方案3】:

    通过下面的 jQuery 代码,你可以在没有任何 API 密钥的情况下找到你当前所在位置的经纬度。让我们试试

     $(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);
            });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-16
      • 2011-08-18
      • 1970-01-01
      • 2017-11-10
      • 1970-01-01
      相关资源
      最近更新 更多