@luxurypluxury

Как узнать страну и город с помощью google maps API?

Нужно узнать страну и город, пока у меня так получается, но есть какой-то способ не из массивов вытаскивать, а сразу получить?
const getGeo = () => {
        if ("geolocation" in navigator) {
            navigator.geolocation.getCurrentPosition((position) => {
                const lat = position.coords.latitude;
                const lng = position.coords.longitude;
                const url = API_URL + lat + "," + lng + "&key=" + API_KEY + "&language=en";
                fetchGeo(url);
            });
            return `${country}, ${city}`;
        } else {
            console.log("Geolocation is not supported by this browser.");
        }
    }

    const fetchGeo = async (url: string) => {
        try {
            const { data } = await axios.get(url);
            const dataArray = data.results[0].formatted_address.split(', ');
            dispatch(setCountry(dataArray[4]));
            dispatch(setCity(dataArray[2]));
        } catch (error) {
            alert('An error occurred while getting the location');
            console.error(error);
        }
    }
  • Вопрос задан
  • 34 просмотра
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы