javascript - 使用地理定位时,我的浏览器没有提示我访问位置
问题描述
我正在编写一段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);
}
解决方案
推荐阅读
- javascript - AngularJS ng-hide/show label/button if length(description) < 20
- server - 在命令行上运行“monit restart all”昨天还在工作
- mysql - SQL - 根据另一个列值设置列值
- javascript - 单击/悬停按钮时显示不同的颜色和消息
- android - 在 react-native Android 发布版本上查看控制台日志
- database - 如何将来自不同主机的两个 MongoDB 合并为一个?
- objective-c - 循环遍历特定文本的文本字段中的值
- manifest - Dynamics 365 数据实体包清单查询数据编码
- jenkins - 使用jenkins上传本地文件到ftp
- api - 无法识别 Spotify API 范围 - 无法访问用户信息