W3C 地理定位 API 没有错误 没有期望也没有结果
Posted
技术标签:
【中文标题】W3C 地理定位 API 没有错误 没有期望也没有结果【英文标题】:W3C geolocation API No errors No expections and No results also 【发布时间】:2012-07-10 07:50:02 【问题描述】:我在这里多次阅读了相同的问题,并修复了重复次数最多的解决方案,并添加了 w3c 规范中提到的错误处理程序,但它没有给我任何异常或错误,也没有结果。我不知道该怎么办?
这是我写的完整代码!
<!DOCTYPE html>
<html>
<head>
<title> HTML5 Egypt User Group - Demos</title>
<script src="http://maps.google.com/maps/api/js?sensor=false">
</script>
<script type="text/javascript">
var lastLat;
var lastLong;
function updateStatus(message)
document.getElementById("status").innerHTML = message;
function loadDemo()
if(navigator.geolocation)
updateStatus("W3C Geolocation is supported in your browser.");
navigator.geolocation.watchPosition(updateLocation,
handleLocationError,
maximumAge:20000);
window.addEventListener("load", loadDemo, true);
function handleLocationError(error)
switch (error.code)
case 0:
updateStatus("There was an error while retrieving your location: " +
error.message);
break;
case 1:
updateStatus("The user prevented this page from retrieving a location.");
break;
case 2:
updateStatus("The browser was unable to determine your location: " +
error.message);
break;
case 3:
updateStatus("The browser timed out before retrieving the location.");
break;
function updateLocation(position)
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
var accuracy = position.coords.accuracy;
var timestamp = position.timestamp;
document.getElementById("latitude").innerHTML = latitude;
document.getElementById("longitude").innerHTML = longitude;
document.getElementById("accuracy").innerHTML = accuracy;
document.getElementById("timestamp").innerHTML = timestamp;
// sanity test... don't calculate distance if accuracy
// value too large
if (accuracy >= 500)
updateStatus("Need more accurate values to calculate distance.");
return;
</script>
</head>
<body>
<button onclick="loadDemo()"> loadDemo()</button>
<input type="text" value="latitude here" id="latitude" />
<input type="text" value="latitude here" id="latitude" />
<input type="text" value="accuracy here" id="accuracy" />
<input type="text" value="timestamp here" id="timestamp" />
<input type="text" value="status here" id="status" />
<span> By Mustafa Adel<span>
</body>
</html>
【问题讨论】:
【参考方案1】:您正在使用输入控件。尝试设置值而不是 innerHTML。
function updateLocation(position)
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
var accuracy = position.coords.accuracy;
var timestamp = position.timestamp;
document.getElementById("latitude").value = latitude;
document.getElementById("longitude").value = longitude;
document.getElementById("accuracy").value = accuracy;
document.getElementById("timestamp").value = timestamp;
// sanity test... don't calculate distance if accuracy
// value too large
if (accuracy >= 500)
updateStatus("Need more accurate values to calculate distance.");
return;
【讨论】:
非常感谢@Maurice,我重写了它,对我来说也没有意义。浏览器不处理position.coords.latitude
的问题我重新测试了最大年龄和超时时间仍然存在!我不知道该怎么办!
首先检查你的浏览器,如果请求返回一个值,用这个演示测试它,html5demos/geo
你可以使用 watchposition() 代替 updateposition ,它会循环请求坐标,我经常使用它,祝你好运:-)以上是关于W3C 地理定位 API 没有错误 没有期望也没有结果的主要内容,如果未能解决你的问题,请参考以下文章