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