【问题标题】:geolocation.getCurrentPosition fails at night -- How to provide own api key?geolocation.getCurrentPosition 在晚上失败——如何提供自己的 api 密钥?
【发布时间】: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


    【解决方案1】:

    您是否尝试过 geolocation.watchPosition 以防万一?

    【讨论】:

    • watchPosition 目前也不起作用,请参阅此处Demo 两天前它还在工作。另请注意,Firefox 示例不起作用Firefox
    【解决方案2】:

    地理功能有什么作用?您是否正在获取用户的位置详细信息(例如城市、地址等)?地理定位服务不应该抛出错误。我猜你正在结合使用 Google Maps 或 Google Places API。有关详细信息,请参见此处:https://developers.google.com/maps/pricing-and-plans/。我遇到了类似的问题,向 Google 添加信用卡解决了我的问题(他们不会向您收费,这只是证明您不是机器人并增加了您的限额。但也有付费计划)

    【讨论】:

    • 我更新了我的问题以包含整个脚本。是的,我确实使用了 Places 和 Geocoding API,但它们通过了我的 API 密钥并且工作正常。如果我让 Firefox 使用我的密钥,地理定位也可以正常工作。我的问题是,如果您正在访问我的网站,我如何才能在“您的”浏览器中包含我的密钥。
    • 您还可以查看我对@treecon 答案的评论。如果firefox live示例不起作用,那么问题肯定出在地理定位服务中。
    猜你喜欢
    • 2021-10-31
    • 1970-01-01
    • 2018-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-24
    • 2016-08-25
    相关资源
    最近更新 更多