我试图通过rails app @ navigator.geolocation.getCurrentPosition
运行localhost:3000
。相关代码取自Google's Geolocation Demo并通过rails。
目前,地理位置偶尔通过Chrome成功(约占10%),始终几乎立即在歌剧中工作,大约80%的时间在Firefox并且总是在Safari中立即失败。
在失败的浏览器中,只有Safari在控制台中返回错误。具体来说,
Access to geolocation was blocked over insecure connection to http://localhost:3000
对于Safari来说这是有意义的,但是根据我的理解,localhost
在其他浏览器(包括Chrome)中列入白名单并应该正常运行。更不用说,它可以一些次 - 并且总是在Opera中。
在失败的实例中,没有任何内容返回到控制台/检查器,getCurrentPosition
方法永远不会返回任何内容,除非我设置了timeout
参数(在这种情况下它会返回超时错误)。 / p>
关于什么可能导致地理位置在某些浏览器上本地工作,其他人不一致,而其他浏览器根本不工作的任何想法?我完全不知道这个 - 任何想法都赞赏。代码
geolocation.js :
var map, infoWindow;
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: -34.397, lng: 150.644},
zoom: 6
});
infoWindow = new google.maps.InfoWindow;
// Try HTML5 geolocation.
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
infoWindow.setPosition(pos);
infoWindow.setContent('Location found.');
infoWindow.open(map);
map.setCenter(pos);
}, function() {
handleLocationError(true, infoWindow, map.getCenter());
});
} else {
// Browser doesn't support Geolocation
handleLocationError(false, infoWindow, map.getCenter());
}
}
function handleLocationError(browserHasGeolocation, infoWindow, pos) {
infoWindow.setPosition(pos);
infoWindow.setContent(browserHasGeolocation ?
'Error: The Geolocation service failed.' :
'Error: Your browser doesn\'t support geolocation.');
infoWindow.open(map);
}
视图/ contact_details / new.html.slim
#map
script(src="/geolocation.js")
<script async defer src="//maps.googleapis.com/maps/api/js?key=AIzaSyBSSvl9zJIZIXXgp7LMYKNU7-l9cwJSAiw&callback=initMap"></script>
答案 0 :(得分:2)
我也遇到过这个。最新的Chrome 65版本(2018年3月)似乎存在一个错误,它使得getCurrentPosition()和watchPosition()不一致超时,而不是返回结果。到目前为止,我一直无法找到解决此问题的方法,因此目前正在等待谷歌的修复。
作为临时措施,我建议通过“超时”#39; value作为getCurrentPosition()调用的第三个参数,以便至少让函数在正常处理一段时间后返回。然后,您可以通过错误处理程序函数将信息转发给用户。
navigator.geolocation.getCurrentPosition(
function (position) {
// on success this will be called.
},
function (err) {
// on error this will be called (including timeout).
},
{
timeout: 5000
}
);
我在这里的示例中使用了类似的方法:Chrome Geolocate Bug
此处有一个错误报告,看起来很相关:Chromium Issue 820945