So i have heard that there is no real clean way to batch requests referenced here to the GooglePlacesAPI
; understood.
But there has to be a work around.
const retrievePlaces = (google, map, request) => {
var places = [];
var newPlaces = []
var service = new google.maps.places.PlacesService(map);
return new Promise(function(resolve, reject) {
service.nearbySearch(request, function(results, status){
if( status == "OK" ){
for (var i = 0; i < results.length; i++) {
var place = results[i];
places.push(place);
}
resolve(places);
}
});
});
}
I use the above function to retrieve my places first (this works fine). Then I use:
const retrieveDetails = ( google, map, places ) => {
var gmap = {
map: map,
google: google
};
var placeIds = places.map(function(place){
return { placeId: place.place_id }
});
var promiseArray = placeIds.map( place => getPlaceDetailsPromise( place, gmap )
.then( res => ({res}) )
.catch( err => ({err}) ) );
Promise.all(promiseArray)
.then(results => {
console.log(results);
});
}
and:
const getPlaceDetailsPromise = (obj, gmap) => new Promise((resolve, reject) => {
var service = new gmap.google.maps.places.PlacesService(gmap.map);
service.getDetails(obj, (place, status) => {
if (status === google.maps.places.PlacesServiceStatus.OK) {
console.log(" Status OK", place);
resolve(place);
} else {
console.log("Not OK");
}
});
});
to attempt to retrieve the details for all the places from the PlaceDetailsAPI
. What sucks is that it actually works to a degree, but it always returns only 9 responses and no more. furthermore they are out of order.
Does anyone have any insight on how it might be possible to retrieve the details for each place?