This question already has an answer here:
Grabbing longitude and Latitude value from a function
                                
                                    (1个答案)
                                
                        
                                6年前关闭。
            
                    
如何获得此JavaScript代码的经度和纬度作为全局变量。我已经尝试了几次,但无法正常工作。

 navigator.geolocation.getCurrentPosition(handle_geolocation_query,handle_errors);

    function handle_errors(error)
    {
        switch(error.code)
        {
            case error.PERMISSION_DENIED: alert("user did not share geolocation data");
            break;
            case error.POSITION_UNAVAILABLE: alert("could not detect current position");
            break;
            case error.TIMEOUT: alert("retrieving position timed out");
            break;
            default: alert("unknown error");
            break;
        }
    }
   function handle_geolocation_query(position){
    }


我在Jquery Mobile和PhoneGap应用程序中使用它。任何帮助将不胜感激,谢谢

根据到目前为止给出的答案我所拥有的

  $(document).ready(function(){
var latitude, longitude;

     navigator.geolocation.getCurrentPosition(handle_geolocation_query,handle_errors);

    function handle_errors(error)
    {
        switch(error.code)
        {
            case error.PERMISSION_DENIED: alert("user did not share geolocation data");
            break;
            case error.POSITION_UNAVAILABLE: alert("could not detect current position");
            break;
            case error.TIMEOUT: alert("retrieving position timed out");
            break;
            default: alert("unknown error");
            break;
        }
    }
   function handle_geolocation_query(position){
   latitude = (position.coords.latitude);
   longitude = (position.coords.longitude);
}
alert(latitude, longitude);
 });


当我提醒它时,它返回“未定义”

这是HTML

<!DOCTYPE html>
<html>
 <head>
<meta charset="UTF-8">
   <title>Untitled Document</title>
<script type="text/javascript" src="js/jquery.js"></script>
   <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
  <link href="http://code.google.com/apis/maps/documentation/javascript/examples/default.css" rel="stylesheet"     type="text/css" />
<script type="text/javascript" src="js/gas-script.js"></script>
 </head>

<body>
 </body>
 </html>

最佳答案

在navigator.geolocation.getCurrentPosition之前:

var latitude, longitude;


内部handle_geolocoation_query(position):

latitude = position.coords.latitude;
longitude = position.coords.longitude;


使得:

var latitude, longitude;
$(document).ready(function(){
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(handle_geolocation_query,handle_errors);
    } else {
        alert('Device probably not ready.');
    }
});
function handle_errors(error) {
    // error handling here
}
function handle_geolocation_query(position){
    latitude = (position.coords.latitude);
    longitude = (position.coords.longitude);
    onPositionReady();
}
function onPositionReady() {
    alert(latitude, longitude);
    // proceed
}

关于javascript - 将纬度和经度分配为全局使用的变量,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18891954/

10-09 13:32