I'm using the Google Maps API in my React App and I've hit a wall. I'm getting the current location of the user and searching the nearby area. I'm then passing all of those place ids to another function that retrieves the detailed of each place. Code:
// Called when searching for treatment centers in radius
const fetchPlaces = () => {
const google = window.google
const service = new google.maps.places.PlacesService(mapRef.current)
const request = {
location: mapRef.current.getCenter(),
radius: 50000,
query: 'addiction treatment centers',
}
service.textSearch(request, (results, status) => {
if (status === google.maps.places.PlacesServiceStatus.OK) {
// Sets the list of places
updateTreatmentLocs(results)
}
})
}
// Gets detailed places for MapList
const getPlacesDetails = async placesArr => {
const google = window.google
const service = new google.maps.places.PlacesService(mapRef.current)
let detailedPlacesArr = await Promise.all(
placesArr.map(currentPlace => {
return new Promise((resolve) =>
service.getDetails({
placeId: currentPlace.place_id,
fields: ['name', 'vicinity', 'formatted_phone_number', 'geometry', 'place_id']
}, (place, status) => {
console.log(currentPlace.place_id)
if (status === google.maps.places.PlacesServiceStatus.OK) {
return resolve(place)
}
console.log("going to return null")
return resolve(null)
}))
})
)
callbackFromHome(detailedPlacesArr)
}
This issue is that when I pass in the array to getPlacesDetails
, most of the places ids passed in return null
. For example, if I pass in 20 different ids from fetchPlaces
, getPlacesDetails
will obtain all 20 but return null
for 13 of them. I have tried using nearbySearch
instead of textSearch
, but it doesn't affect the result. Any help is appreciated!
question from:
https://stackoverflow.com/questions/65895176/why-is-places-details-search-using-place-id-returning-null 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…