Skip to content

use getGeocode before usePlacesAutocomplete #1105

@WJKwok

Description

@WJKwok

I want to use getGeocode to get latlng to input as requestOptions within usePlaceAutocomplete

something like that:

const CityPage = () => {
	const router = useRouter();
	const { cityName } = router.query;
	const [location, setLocation] = useState<LatLng>();

	getGeocode({ address: cityName }).then((results) => {
		const result = getLatLng(results[0]);
		setLocation(result)
	});

	const {
		ready,
		value,
		suggestions: { status, data },
		setValue,
		clearSuggestions,
	} = usePlacesAutocomplete({
		requestOptions: {
			location
			/* Define search scope here */
		},
		callbackName: 'initMap',
		debounce: 300,
	});

However, next.js complains about ReferenceError: window is not defined. I understand that google has not been initialised.. and the issue would be fixed if useGeocode is used after usePlacesAutocomplete.. but i need it this way and don't know how to go about it. Any advice?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions