Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
178 views
in Technique[技术] by (71.8m points)

javascript - Why is places details search using place id returning null?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...