ErrorPro / react-google-autocomplete

React components for google places API.
MIT License
473 stars 114 forks source link

There is a way to specified `fields` using usePlacesAutocompleteService? #107

Closed narcello closed 3 years ago

narcello commented 3 years ago

I'm using usePlacesAutocompleteService and I wanna specified fields to return in predictions. Is it possible? Or even use the getDetails

ErrorPro commented 3 years ago

For the AutocompleteService only these options available, no fields option available. Regarding the getDetails service, to be able to call this method you need to have different service to be available. I was going to add initiation of this service and bound it together with usePlacesAutocompleteService. If you could give me a bit more context about why you need to do I could build this ASAP. @narcello

ErrorPro commented 3 years ago

Closing this issue for now but I'd love to hear back from you @narcello

adybuciuman commented 3 years ago

I have the same problem. In my case, I need getDetails in order to fetch the coordinates. Besides city, region and country I need also latitude and longitutde and I see that getPlacePredictions() doesn't return the coordinates.

ErrorPro commented 3 years ago

@adybuciuman Hey! getDetails is a different service(places-service) from google API. The mentioned hook only works with the places-autocomplete-service. I had an idea to initiate the places-service instance as well inside usePlacesAutocompleteService and return it so that anyone can use it as part of usePlacesAutocompleteService hook. Let me know if that's something you' like to have to tell me more about how you would like to use it. Thanks!

narcello commented 3 years ago

@ErrorPro Thanks for answer me. My goal is get the cities and I want to get coordinates and formatted_address as well, to fill address form automatically with postal code, street, venue, etc.

ErrorPro commented 3 years ago

@narcello @adybuciuman hey guys! If the usePlacesAutocompleteService returns placesService which you can call at any place in your component will that help you to solve your cases? for example

const { placePredictions, getPlacePredictions, placesService } =
    usePlacesService({
      apiKey: process.env.REACT_APP_GOOGLE,
    });

useEffect(() => {
  placesService. getDetails({ placeId: placePredictions[0].place_id, fields: ['geometry.location'] })
    .then(res => console.log(res))
}, [placePredictions]);

or you can call this line when user selects a location from your autocompleteonClick={() => placesService. getDetails({ placeId: el.place_id, fields: ['geometry.location'] })}. In this case you can use an autocomplete session feature and only pay for a single session.

Let me know if there's any other way you'd prefer it to be. Thanks!

thebiltheory commented 3 years ago

How can we get the coordinates in the meantime?

ErrorPro commented 3 years ago

@thebiltheory You can get the placeid from the placePredictions and then you need to create a placesService yourself to get it. It's a bit of hassle and overhead I know. I hope that the proposition I described above will find its support among people who use this library so I can implement that ASAP. I am trying to see if there's any other option to achieve the same result but it seems there's not. Let me know your thoughts

ErrorPro commented 3 years ago

@thebiltheory @narcello I just added this functionality to the lib. You can use it as I described here. Also there's a way to use a sessionToken to group requests.

narcello commented 3 years ago

@ErrorPro Nice, thanks. I just seen and I'll test it today. Thank you, I'll let you know about the tests :)

narcello commented 3 years ago

@ErrorPro Worked, but I had to pass a function callback to get the result from getDetails, little bit different from here but it's fine man, thanks again :)

ErrorPro commented 3 years ago

@narcello Yes it's a bit different cuz I supposed that the getDetails would return a promise but it does not :( I will write a wrapper around its interface to promisify sometimes later, it will be backwards-compatible. Hope the current implementation will work for a while :) Cheers!

narcello commented 3 years ago

@ErrorPro Don't worry, it's working :) thank you again 🎉