首页 > 解决方案 > 使用地理定位时,我的浏览器没有提示我访问位置

问题描述

我正在编写一段javascript,它使用geolocation api检索用户的位置,并使用google maps 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);
}

标签: javascriptgeolocation

解决方案


您需要为您的页面提供服务,而HTTPS不是HTTPGeolocationAPI:

安全上下文

此功能仅在安全上下文 (HTTPS)、部分或所有支持的浏览器中可用。

这里以供参考。


推荐阅读