navigator.geolocation.getCurrentPosition不调用回调函数

时间:2015-07-01 02:17:46

标签: javascript

任何人都可以告诉我为什么此代码无法在Chrome中运行? (我读了关于Firefox的bug并尝试了所有提出的解决方案) 用户被问及是否允许.....这是有效的,他们点击是.....但回调函数永远不会被调用...我认为添加超时会工作,但没有运气。我已经在手机和手机上进行了测试桌面和同样的失败。

请帮助!!

window.onload = getMyLocation;

function getMyLocation(){
    if (navigator.geolocation){
        navigator.geolocation.getCurrentPosition(displayLocation, displayError, {timeout:5000});
    }else{
        alert("Brower does not support Geolocation");
    }
}

function displayError(error){
    var errorTypes={
        0:"Unknown Error",
        1:"Permission Denied by User",
        2:"Position is not Available",      
        3:"Request Timed Out"
    }
    var errorMessage=errorTypes[error.code];
    if (error.code==0 || error.code==2){
        errorMessage=errorMessage + " " + error.message;
    }

    var div = getElementById("location");
    div.innerHTML=errorMessage
}

function displayLocation(position){
    var latitude = position.coords.latitude;
    var longitude = position.coords.longitude;

    var div = getElementById("location");
    div.innerHTML="latitude:" + latitude + " longitude:" + longitude;

}

1 个答案:

答案 0 :(得分:0)

var div = getElementById("location");

=> var div = document.getElementById("location");

你错过了一些分号:

div.innerHTML=errorMessage;

var errorTypes={
    0:"Unknown Error",
    1:"Permission Denied by User",
    2:"Position is not Available",      
    3:"Request Timed Out"
};

希望这有帮助! :)