我一直在测试一个使用HTML5地理定位功能返回经度和纬度值的简单code fragment:
var x = document.getElementById("demo");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition, showError);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
x.innerHTML = "Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
}
function showError(error) {
switch(error.code) {
case error.PERMISSION_DENIED:
x.innerHTML = "User denied the request for Geolocation."
break;
case error.POSITION_UNAVAILABLE:
x.innerHTML = "Location information is unavailable."
break;
case error.TIMEOUT:
x.innerHTML = "The request to get user location timed out."
break;
case error.UNKNOWN_ERROR:
x.innerHTML = "An unknown error occurred."
break;
}
}
<p>Click the button to get your coordinates.</p>
<button onclick="getLocation()">Try It</button>
<p id="demo"></p>
我直接在Google Chrome的“试用”部分测试了代码,它返回了“error.PERMISSION_DENIED”。但是,当我在本地主机下的XAMPP中部署它时,它起作用了。请注意,我已经设置谷歌浏览器,以共享基于此documentation的位置详细信息。
但这段代码在火狐中返回'error.POSITION_UNAVAILABLE‘,即使部署在XAMPP中,并且当我同意在火狐中共享我的位置时也是如此。
在Google Chrome中(直接调用时和部署在localhost下时)以及通过Firefox访问时,是什么导致了这种不一致的行为?
发布于 2016-08-09 14:48:40
https://stackoverflow.com/questions/38853970
复制相似问题