【问题标题】:Limit Results using Google Geocode API使用 Google Geocode API 限制结果
【发布时间】:2018-05-17 16:42:17
【问题描述】:

我正在开展一个需要使用免费 Google Geocoding API 的项目。我需要使用 Geocoding API 查询的结果来验证查询本身并确保所有字段都正确。

我一直在尝试查看是否存在与 Bing api maxResults 参数平行的选项,但似乎在文档中找不到任何内容。 maxResults 参数限制了返回的地址数量,因此使作业花费的时间大大减少。

这里是 Bing API 文档,其中包含有关 maxResults 参数的信息。 (ctrl+f): https://msdn.microsoft.com/en-us/library/ff701714.aspx

Google Geocoding API 的概述可以在这里找到: https://developers.google.com/maps/documentation/geocoding/intro

【问题讨论】:

    标签: google-maps-api-3 bing-api geocode


    【解决方案1】:

    您的帖子很混乱,因为您将其标记为google-maps-api-3,这在技术上是针对Google Maps Javascript API v3,但您提供的文档是针对Web Services Geocoder API

    但要回答您的问题,目前此功能不适用于 Web 服务地理编码器 API,并且无法限制最大结果。

    限制 Javascript API 地理编码服务 的返回结果也不可用,但有一种解决方法可以限制您将在 客户端显示的结果数量(前端)。您可以通过循环遍历每个结果并将循环限制为您想要的数字来做到这一点。

    这是示例代码的一部分,我在其中执行了for-loop,并设置了一个变量len(用于长度)。

    var s_markers = [];
    var map;
    function initialize() {
      var currentarea = { lat: 14.5393988, lng: 121.0521586};
      map = new google.maps.Map(document.getElementById('map'), {
        zoom: 2,
        center: {lat:0,lng:0},
        disableDefaultUI: true
      });
    
      var address = 'google maps'; // just used google maps since it returns multiple results
      var geocoder = new google.maps.Geocoder();
      geocoder.geocode({ 'address': address },function(results, status) {
        // var len = results.length;
        var len = 2; //declare here the number of wanted result
        for (var x=0; x<len; x++){
          var pos = results[x].geometry.location;
          map.panTo(pos);
          var s_mark = new google.maps.Marker({
            position: pos,
            title: results[x].formatted_address,
            map: map
          });
          s_markers.push(s_mark);
        }
      });
    }
    

    最初我在这里使用var len = results.length 将所有结果显示为标记。但要限制显示的结果,您可以将其设置为var len = 2var len = 1。这里的原始退货数量是3

    你可以在这里看到完整的代码:http://jsbin.com/nagadis/edit?html,js,output

    但是,如果您想要这个可以限制结果数量的功能,最好的办法是在Google Issue tracker

    中提交功能请求

    希望这有帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-07
      • 1970-01-01
      • 1970-01-01
      • 2021-12-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多