我有一个带有某个地方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>");
}
}
}
}
但是输出不是我所期望的。控制台.log没问题,但追加仅显示来自 API 的一些数据。演示
似乎 api 需要一些时间来处理。不知道如何处理。
你尝试在客户端 JavaScript 代码上执行一堆位置详细信息请求。您应该注意的最重要的事情是客户端速率配额的存在。它在以前版本的文档中提到过,但是,我在新的Google地图平台文档中找不到它。不确定谷歌是否会在 2018 年 7 月 16 日正式推出新定价模式后取消此限制。
在旧文档中是这样提到的:
无论有多少用户共享同一项目,服务请求都按用户会话进行速率限制。首次加载服务 API 时,系统会为您分配初始请求配额。使用此配额后,API 会每秒对其他请求强制实施速率限制。如果在某个时间段内发出了太多请求,API 将返回OVER_QUERY_LIMIT响应代码。每个会话的速率限制阻止对批处理请求使用客户端服务。对于批处理请求,请使用地图 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/
我希望这有帮助!