我想使用Google Places API获取有关地点的一些数据。 事情是,我想从我要查找的地区的每个城市获得超过1000条记录的数据。使用Google Maps API每次请求延迟Ajax功能
我正在寻找匹萨店,而且我想要我定义的区域中的所有匹萨店。所以,我有一个阵列是这样的:
['Pizzeria+Paris','Pizzeria+Marseille','Pizzeria+Nice','Pizzeria+Toulouse']
我的目标是使单个请求,然后等待3秒(或更多),然后处理所述第二请求。我正在使用Lodash库来帮助我迭代。
这里是我的代码:
function formatDetails(artisan){
var latitude = artisan.geometry.location.lat;
var longitude = artisan.geometry.location.lng;
var icon = artisan.icon;
var id = artisan.id;
var name = artisan.name;
var place_id = artisan.place_id;
var reference = artisan.reference;
var types = artisan.types.toString();
$('#details').append('<tr>'+
'<td>'+latitude+'</td>'+
'<td>'+longitude+'</td>'+
'<td>'+icon+'</td>'+
'<td>'+id+'</td>'+
'<td>'+name+'</td>'+
'<td>'+place_id+'</td>'+
'<td>'+reference+'</td>'+
'<td>'+types+'</td>'+
'</tr>');
}
var getData = function(query, value){
$.ajax({
url: query,
type: "GET",
crossDomain: true,
dataType: "json",
success: function(response) {
var artisan = response.results;
console.log(artisan);
for (var i = 0; i < artisan.length; i++){
formatDetails(artisan[i]);
setTimeout(function(){console.log('waiting1');},3000);
}
setTimeout(function(){console.log('waiting2');},3000);
},error: function(xhr, status) {
console.log(status);
},
async: false
});
}
$(document).ready(function(){
var places =
['Pizzeria+Paris','Pizzeria+Marseille','Pizzeria+Nice','Pizzeria+Toulouse'];
_.forEach(places, function(value, key) {
var proxy = 'https://cors-anywhere.herokuapp.com/';
var target_url = 'https://maps.googleapis.com/maps/api/place/textsearch/json?query='+value+'&key=AIzaSyAClTjhWq7aFGKHmUwxlNUVBzFpIKTkOrA';
var query = proxy + target_url;
getData(query, value);
});
});
我已经尝试了很多解决方案我在计算器发现,但没有一个人的工作,否则我可能会做他们错了。
感谢您的帮助!
'我已经尝试了很多我在stackoverflow上找到了哪些解决方案? –
我试过你可以在这里找到: https://stackoverflow.com/questions/33395048/set-a-delay-in-ajax-call https://stackoverflow.com/questions/18965768/set-a-延迟重复-jquery-ajax-function https://stackoverflow.com/questions/17332976/delay-in-ajax-success-not-working https:// stackoverflow。com/questions/40829915/i-want-to-delay-jquery-ajax-successful- – yofisim