我正在编写一段javascript,它使用地理位置API检索用户的位置,并使用谷歌地图API在地图上显示它。我写了下面的代码。它似乎在本地工作正常,但是一旦我在我的网站上托管代码,就会有用户位置(http://lucakleijweg.nl/myLoc.html(的提示。你能帮我弄清楚为什么吗?
//getting the user's location
window.onload = getMyLocation();
function getMyLocation(){
if (navigator.geolocation){
navigator.geolocation.getCurrentPosition(displayLocation, displayError);
} else {
alert ("oops, no geolocation support");
}
}
function displayLocation(position){
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
var div = document.getElementById("location");
div.innerHTML = "you are at Latitude:" + latitude + ", Longitude:" + longitude;
div.innerHTML += " (with " + position.coords.accuracy + " meters accuracy)";
//calculate distance from HQ using user's coordinates
var km = computeDistance(position.coords, ourCoords);
var distance = document.getElementById("distance");
distance.innerHTML = "you are " + km + " km away from the Wickedly Smart HQ";
showMap(position.coords);
}
您需要通过
HTTPS
而不是HTTP
Geolocation
API 来提供您的页面:安全上下文
此功能仅在安全上下文 (HTTPS( 中部分或所有支持浏览器中可用。
查看此处以供参考。