Google地图未在Chrome / Firefox浏览器中显示

时间:2015-08-12 10:15:38

标签: browser geolocation

我正在尝试在浏览器中测试HTML 5中的Geolocation。但出于某种原因,它并没有显示我的地方的纬度和长度。甚至不是错误信息

为什么lat和long不显示。请协助。我试过Chrome和FF。同样的结果。是不是一直显示结果还是需要时间

window.onload = checkLocation;
function checkLocation() {
//we put the if condition that will check if the browser supports geplocationn feature or not
if(navigator.geolocation) {
    //if yes, then call the getCurremtPosition method and pass in a handler function ()

    navigator.geolocation.getCurrentPosition(showLocation, showError);

    // And we’re calling the geolocation object’s getCurrentPosition method with one argument, the success callback.
}else {

    alert(' Hey No locator for your browser');
}
}

function showLocation(position) {


//position parameter is passed so as to get getCurrentPosition’s handler is passed a position, which contains the latitude and longitude of your location

// get the lat and lon of location from the cords object
var lat = position.coords.lat;
var lon = position.coords.lon;

var content = document.getElementById('myLocation');

content.innerHTML = ' Your latitude position is: ' + lat + ' and your Longtitude position is:  ' + lon + '</br>';

}

function showError(error) {
var errorList = {
//We create an object with three properties named zero to three. 
//These properties are strings with an error message we want to associate with each code

    0: 'Unknown error',
    1: 'Permission denied by user',
    2: 'Position not available',
    3: 'Request timed out'
};

var errorMsg = errorList[error.code];// use error code property to access the error list []

if(error.code == 0 || error.code == 2) {
    errorMsg = errorMsg + " " + error.message; // use erroo message property ro access the value in error list
}
var content = document.getElementById('myLocation');

content.innerHTML = erroMsg;
}

1 个答案:

答案 0 :(得分:0)

BTW我发现了这个问题。在调用map api脚本时,请确保url以//而不是http开头。让google决定如何提供api文件。