这是一个很长的问题,可能只是一个很简短的答案。geolocation.getCurrentPosition在晚上失败 - 如何提供自己的API密钥?
在我的轨道5的应用程序,我用这个非常基本的JS实现地理位置API来获取来自浏览器的用户的位置:
更新:这是它使用geocomplete插件整个脚本(https://ubilabs.github.io/geocomplete/):
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=<%=ENV['GOOGLE_MAPS_API_KEY']%>&language=<%=I18n.locale%>&libraries=places"></script>
<script>
$(function(){
var geolocation = null;
var lati = null;
var longi = null;
//start by drawing a default map
nogeoloc();
function nogeoloc() {
geo(lati,longi);
console.log("no geolocation");
}
// check for geolocation support
if (window.navigator && window.navigator.geolocation) {
geolocation = window.navigator.geolocation;
}
if (geolocation) {
geolocation.getCurrentPosition(success, error, positionOptions);
}
var positionOptions = {
enableHighAccuracy: true,
timeout: 10 * 1000, // 10 seconds
};
//in case of user permission, center the map at user' location
function success(position) {
lati = position.coords.latitude;
longi = position.coords.longitude;
geo(lati,longi);
console.log("geo data obtained");
}
// if denied permission or error, do nothing
function error(positionError) {
console.log("error");
console.log(positionError);
}
// map drawing using the geocomplete library
function geo(lati,longi){
var myloc = false;
if(lati && longi) {
myloc = [lati, longi];
}
var options = {
map: ".map_canvas",
mapOptions: {
scrollwheel: true
},
location: myloc ,
details: "form",
detailsAttribute: "geocompname",
markerOptions: {
draggable: true
}
};
$("#geocomplete").geocomplete(options);
var geocoder = new google.maps.Geocoder();
var map = $("#geocomplete").geocomplete("map");
var marker = $("#geocomplete").geocomplete("marker");
if(lati && longi) {
map.panTo({ lat: lati, lng: longi});
marker.setPosition({ lat: lati, lng: longi});
$("input[geocompname=lat]").val(lati);
$("input[geocompname=lng]").val(longi);
geocoder.geocode({'location': {lat: lati, lng: longi}}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[0]) {
$("input[geocompname=formatted_address]").val(results[0].formatted_address);
}
}
});
}
$("#geocomplete").bind("geocode:dragged", function(event, latLng){
$("input[geocompname=lat]").val(latLng.lat());
$("input[geocompname=lng]").val(latLng.lng());
map.panTo(latLng);
geocoder.geocode({'latLng': latLng }, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[0]) {
$("input[geocompname=formatted_address]").val(results[0].formatted_address);
}
}
});
});
}
});
</script>
的莫名其妙搞笑问题是这个实现在白天工作正常(我在德国),但是在晚上都会在Firefox和Chrome中返回错误:)。
在Firefox返回错误代码是:{ code: 2, message: "Unknown error acquiring position" }
,Chrome浏览器打印的更多信息:message: "Network location provider at 'https://www.googleapis.com/' : Returned error code 403."
周围的Googling,事实证明,403是指所提供的API密钥的使用限制达到。这解释了为什么我的代码在白天工作,并在晚上开始失败:)。
为了测试这一点,我打开Firefox的配置和插入我自己的谷歌API密钥geo.wifi.uri,瞧它的工作原理。
现在我的问题(S):
1)这个问题出现在谷歌自己的浏览器(Chrome),甚至Mozilla的活生生的例子失败(https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/Using_geolocation),一个可以期待谷歌/ Mozilla浏览器等,以解决此问题很快通过增加请求限制?
2)请求经由网站访问者的浏览器发送。有没有办法将我自己的谷歌密钥传递给地理位置api,以便浏览器在请求中使用它而不是默认的?
3)上面的代码工作正常,在我的三星S6手机上的Chrome浏览器,但它并没有使用三星的互联网浏览器相同的手机上运行。任何提示这里有什么问题?
watchPosition不起作用此刻要么,看这里[演示](https://www.audero.it/demo/geolocation-api-demo.html) 有人两天前工作。还要注意的是,Firefox的例子不起作用[火狐](https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/Using_geolocation) – pion