获取一堆物品的地点ID地址

时间:2018-07-03 13:46:20

标签: javascript jquery google-maps google-maps-api-3 google-places-api

我有一个带有某个地方ID的json对象。现在,我试图获取每个商品的相应地址。

这是我正在尝试的:

<div id="places" class="places"></div>
<script>
  function initialize() {
    json_data = jQuery.parseJSON(...);

    for (i = 0; i < json_data.length; i++) {
      console.log(json_data[i].id);
      console.log(json_data[i].key);

      var request = {
        placeId: json_data[i].key
      };

      service = new google.maps.places.PlacesService(document.getElementById("places"));
      service.getDetails(request, callback);

      function callback(place, status) {
        if (status == google.maps.places.PlacesServiceStatus.OK) {
          $(".places").append("<p>" + request.placeId + " - " + place.formatted_address + "</p>");
        }
      }
    }
  }

但是输出不是我期望的。 console.log可以,但是附录仅显示API中的一些数据。 DEMO

似乎api需要一些时间来处理。不确定如何处理。

1 个答案:

答案 0 :(得分:1)

您尝试在客户端JavaScript代码上执行一堆地点详细信息请求。您应该知道的最重要的事情是客户端速率配额的存在。在文档的先前版本中已经提到了它,但是,在新的Google Maps Platform文档中找不到它。不确定Google是否会在新定价模式正式发布后的2018年7月16日之后取消此限制。

在旧文档中是这样提到的:

  

服务请求在每个用户会话中均受速率限制,无论有多少用户共享同一项目。首次加载服务API时,会为您分配初始的请求配额。使用此配额后,API会每秒对其他请求实施速率限制。如果在特定时间段内提出了太多请求,则API返回OVER_QUERY_LIMIT响应代码。每个会话的速率限制禁止将客户端服务用于批处理请求。对于批处理请求,请使用Maps API Web服务。

每个会话配额通常允许10个请求的存储桶,一旦存储桶为空,您每秒只能执行1个请求。因此,您必须检查响应的状态,并在延迟1秒后检查OVER_QUERY_LIMIT重复请求的情况。

我修改了您的代码以解决您的问题

function initialize() {
    var json_data = jQuery.parseJSON('[ { "id": 146, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 145, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 144, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 143, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 142, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 141, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 140, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 139, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 138, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 137, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 136, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 135, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 134, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 133, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 132, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 131, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 130, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 129, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 128, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 127, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 126, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 125, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 124, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 123, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 122, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 121, "key": "ChIJ2QhxhMHOHg0RF8zBGoV9sVQ" }, { "id": 120, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 119, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 118, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 117, "key": "ChIJwVPhxKtlJA0RvBSxQFbZSKY" }, { "id": 116, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 115, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 114, "key": "ChIJwVPhxKtlJA0RvBSxQFbZSKY" }, { "id": 113, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 112, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 111, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 110, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 109, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 108, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 107, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 106, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 105, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 104, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 103, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 102, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 101, "key": "ChIJEYNl2y3JHg0R9zyZw3xCJ6A" }, { "id": 100, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 99, "key": "ChIJ9QQaFsHOHg0ROav_i9VD3V4" }, { "id": 98, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 97, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 96, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 95, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 94, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 93, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 92, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 91, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 90, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 89, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 88, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 87, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 86, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 85, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 84, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 83, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 82, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 81, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 80, "key": "ChIJSfWKOnvMHg0RqLLi-22LWnI" }, { "id": 79, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 77, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 76, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 75, "key": "ChIJwVPhxKtlJA0RvBSxQFbZSKY" }, { "id": 74, "key": "ChIJ4zGTOfhmJA0RfmcqcLKlVKk" }, { "id": 73, "key": "ChIJQRdO0HrMHg0RThqjF9M9-4w" }, { "id": 72, "key": "ChIJbdGsShT5Ig0RuaZCe0dkRWM" }, { "id": 71, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 70, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 69, "key": "ChIJr367qitvJA0Rvy5Oh88YCUE" }, { "id": 68, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 67, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 66, "key": "ChIJwVPhxKtlJA0RvBSxQFbZSKY" }, { "id": 65, "key": "ChIJK8leGat-GA0R6uUjP8gMizY" }, { "id": 64, "key": "ChIJwVPhxKtlJA0RvBSxQFbZSKY" }, { "id": 63, "key": "ChIJwVPhxKtlJA0RvBSxQFbZSKY" }, { "id": 62, "key": "ChIJzVzD6z42Iw0RmkTnIYjAv_Y" }, { "id": 61, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 60, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 59, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 58, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 57, "key": "ChIJwVPhxKtlJA0RvBSxQFbZSKY" }, { "id": 56, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 55, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 54, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 53, "key": "ChIJwVPhxKtlJA0RvBSxQFbZSKY" }, { "id": 52, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 51, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 50, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 49, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 48, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 47, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 46, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 45, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 44, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 43, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 42, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 41, "key": "ChIJwVPhxKtlJA0RvBSxQFbZSKY" }, { "id": 40, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 39, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 38, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 37, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 36, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 35, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 34, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 33, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 32, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 31, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 30, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 29, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 28, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 27, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 26, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 25, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 24, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 23, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 22, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 21, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 20, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 19, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 18, "key": "ChIJVT4alwcuGQ0RoHvco1jDCQ0" }, { "id": 17, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 16, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 15, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 14, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 13, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 12, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 11, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 10, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 9, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 8, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 7, "key": "ChIJyxnnULr4Gg0R9zGpUZLEL1M" }, { "id": 6, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 5, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 4, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 3, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" }, { "id": 2, "key": "ChIJS2ITKK8zGQ0R4ECQ5L3rAAU" }, { "id": 1, "key": "ChIJO_PkYRozGQ0R0DaQ5L3rAAQ" } ]');
            
            var delayFactor = 0;
            var service = new google.maps.places.PlacesService(document.getElementById("places"));

            function m_get_places (request) {
                service.getDetails(request, function(place, status) {
                    if (status == google.maps.places.PlacesServiceStatus.OK) {
                        $(".places").append("<p>" + request.placeId + " - " + place.formatted_address + "</p>");
                    } else if (status === google.maps.places.PlacesServiceStatus.OVER_QUERY_LIMIT) {
                        delayFactor++;
                        setTimeout(function () {
                            m_get_places(request);
                        }, delayFactor * 1000);
                    } else {
                        console.log(status);
                    }
                });
            } 
         
            for (i = 0; i < json_data.length; i++) { 
                console.log( json_data[i].id );
                console.log( json_data[i].key );
                
                var request = {
                    placeId: json_data[i].key
                };
            
                m_get_places(request);
            }       
        }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDztlrk_3CnzGHo7CFvLFqE_2bUKEq1JEU&libraries=places&callback=initialize"></script>
<div id="places" class="places"></div>

您也可以在jsfiddle上找到此示例:https://jsfiddle.net/xomena/kvq07xyr/

我希望这会有所帮助!