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? |
0 replies
|
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. |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
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
All reactions