【发布时间】:2017-05-03 22:27:12
【问题描述】:
这是一个很长的问题,答案可能很简短。
在我的 rails 5 应用程序中,我使用地理定位 api 的这个相当基本的 JS 实现从浏览器获取用户位置:
更新:这是使用 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."
四处搜索,发现 403 表示已达到提供的 api 密钥的使用限制。这解释了为什么我的代码在白天工作并在晚上开始失败:)。
为了测试这一点,我打开了 firefox 配置并将我自己的 google api 密钥插入到 geo.wifi.uri 中,瞧,它可以工作了。
现在我的问题:
1) 这个问题出现在google自带的浏览器(Chrome)中,连Mozilla的live example都失败了(https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/Using_geolocation),可以期待google/mozilla等通过增加请求限制很快解决这个问题吗?
2) 请求通过站点访问者的浏览器发送。有没有办法将我自己的 google 密钥传递给 geolocation api,以便浏览器在请求中使用它而不是默认的?
3) 上述代码在我的三星 s6 手机上的 chrome 浏览器中运行良好,但在使用三星互联网浏览器的同一部手机上却无法运行。任何提示这里出了什么问题?
【问题讨论】:
标签: javascript ruby-on-rails google-chrome firefox geolocation