我正在开发一个Phone Gap应用程序。在该应用程序中,我想显示当前用户的位置。为此,我使用了HTML5地理定位。当我单击“获取位置”按钮时,它不会显示位置。它也不会显示错误。按下“获取位置”按钮后,它只显示处于选定状态的按钮。
<!DOCTYPE html>
<html>
<body>
<button onclick="getLocation()">Get Location</button>
<script>
var x = document.getElementById("demo");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition, showError);
}
else { x.innerHTML = "Geolocation is not supported by this browser."; }
}
function showPosition(position) {
x.innerHTML = "Latitude: " + position.coords.latitude + "<br>Longitude: " + position.coords.longitude;
}
function showError(error) {
switch (error.code) {
case error.PERMISSION_DENIED:
x.innerHTML = "User denied the request for Geolocation."
break;
case error.POSITION_UNAVAILABLE:
x.innerHTML = "Location information is unavailable."
break;
case error.TIMEOUT:
x.innerHTML = "The request to get user location timed out."
break;
case error.UNKNOWN_ERROR:
x.innerHTML = "An unknown error occurred."
break;
}
}
</script>
</body>
</html>
答案 0 :(得分:1)
您正在引用一个不存在的元素。 使用id =&#34; demo&#34;为你的html主体添加一个div,如下所示:
<div id="demo"></div>
在jsfiddle:http://jsfiddle.net/wU6AE/
上查看