1

我如何获取此 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>
4

2 回答 2

3

在 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
}     
于 2013-09-19T10:29:15.007 回答
0

在下面提到的代码中,lat,lng 应该在当前文件中全局可用,

$(document).ready(function(){
var lat, lng;
navigator.geolocation.getCurrentPosition(handle_geolocation_query,handle_errors);  
function handle_geolocation_query(position){  
    lat = position.coords.latitude;    
    lng =  position.coords.longitude; 
}  
});
于 2013-09-19T10:27:12.830 回答