Geolocation W3C地理定位API无错误无预期也无结果
我已经在这里多次阅读了相同的问题,修复了重复最多的解决方案,并添加了w3c规范中提到的错误处理程序,但它没有给我任何异常或错误,也没有结果。我不知道我该怎么办 这是我写的完整代码Geolocation W3C地理定位API无错误无预期也无结果,geolocation,w3c-geolocation,Geolocation,W3c Geolocation,我已经在这里多次阅读了相同的问题,修复了重复最多的解决方案,并添加了w3c规范中提到的错误处理程序,但它没有给我任何异常或错误,也没有结果。我不知道我该怎么办 这是我写的完整代码 <!DOCTYPE HTML> <html> <head> <title> HTML5 Egypt User Group - Demos</title> <script src="http://maps.google.com/maps/api/j
<!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>
HTML5埃及用户组-演示
var lastLat;
var lastLong;
函数updateStatus(消息){
document.getElementById(“status”).innerHTML=消息;
}
函数loadDemo(){
if(navigator.geolocation){
updateStatus(“您的浏览器支持W3C地理定位”);
navigator.geolocation.watchPosition(updateLocation,
手动定位错误,
{maximumAge:20000});
}
}
addEventListener(“加载”,loadDemo,true);
函数handleLocationError(错误){
开关(错误代码){
案例0:
updateStatus(“检索您的位置时出错:”+
错误消息);
打破
案例1:
updateStatus(“用户阻止此页面检索位置”);
打破
案例2:
updateStatus(“浏览器无法确定您的位置:”+
错误消息);
打破
案例3:
updateStatus(“浏览器在检索位置之前超时”);
打破
}
}
函数更新位置(位置){
变量纬度=位置坐标纬度;
var经度=position.coords.longitude;
var精度=位置坐标精度;
var timestamp=position.timestamp;
document.getElementById(“纬度”).innerHTML=纬度;
document.getElementById(“经度”).innerHTML=经度;
document.getElementById(“准确性”).innerHTML=准确性;
document.getElementById(“timestamp”).innerHTML=时间戳;
//健全测试…如果准确,不计算距离
//价值太大
如果(精度>=500){
updateStatus(“需要更精确的值来计算距离”);
返回;
}
}
loadDemo()
穆斯塔法·阿德尔
您正在使用输入控件。尝试设置该值而不是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,它会循环请求coords,我通常用它,祝你好运:-)