简体   繁体   English

查找当前位置的纬度和经度

[英]Find latitude and longitude of current location

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.它在 Chrome 中运行良好,但在 Mozilla 中不运行。 There is no alert error message either.也没有警报错误消息。

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通过使用下面的 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);
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 计算当前位置经纬度 - Calculate the current location longitude and latitude 如何使用谷歌地图查找当前位置(经度和纬度)? - How to find the current location(longitude and latitude) using Google Map? 获取用户当前位置的经纬度 - Fetch latitude and longitude on user's current location 如何在OpenLayers中获取用户的当前位置以及纬度和经度 - how to get the current location of the user along with latitude and longitude in OpenLayers 如何获取当前位置(经度和纬度)并将其发送到反向地理编码API? - How to get current location (latitude and longitude) and send it to an reverse geocoding API? 检测到当前位置后如何在mapbox中获取经度和纬度? - How to get longitude and latitude in mapbox after detect current location? 如何使用经纬度从json数据中找到最近的位置 - How to find nearest location using latitude and longitude from a json data 开放层中当前位置的纬度和经度 - latitude and longitude of current position in openlayer 使用Java程序查找具有指定半径的给定纬度(即位置)周围的纬度和纬度值 - Find surrounding the latitude and latitude values of given latitude and longitude(i.e: location) with specified radius using java program 通过纬度经度查找topojson特征 - Find topojson feature by latitude longitude
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM