使用地理定位时,我的浏览器不提示我访问位置
My browser doesn't prompt me for access to location when using geolocation
我正在写一段 javascript,它使用地理定位 api 检索用户的位置,并使用 google 地图 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:
提供页面
Secure context
This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.
参考here。
我正在写一段 javascript,它使用地理定位 api 检索用户的位置,并使用 google 地图 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:
Secure context
This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.
参考here。