Replies: 2 comments
-
Add the following to the autocomplete field: useEffect(() => {
if (places && defaultValue && inputRef.current && placeAutocomplete) {
const service = new places.PlacesService(inputRef.current);
service.findPlaceFromQuery(
{
query: defaultValue,
fields: ["geometry", "name", "formatted_address"],
},
(results) => {
if (results?.length && results[0]) {
onPlaceSelect(results[0]);
}
},
);
}
}, [defaultValue, onPlaceSelect, placeAutocomplete, places]); is this the right approach? |
Beta Was this translation helpful? Give feedback.
-
It looks like this should work. However, it might be more economic to save the lat/lng in the db/server when the address enters your system. That way you don't have to do an "initial" request every time the page is viewed since it is not very common for an address to to be in a different place very often. This way you could set the default text value to the input and also display the location on the map with the previous saved location and then just react to actual changes in the input field. |
Beta Was this translation helpful? Give feedback.
-
My app has an address field that shows the address on the map, with an autocomplete field.
Is there a way to set the initial location to the value that I get from the server?
Thanks
Beta Was this translation helpful? Give feedback.
All reactions