shukerullah / react-geocode

A React module to transform a description of a location (i.e. street address, town name, etc.) into geographic coordinates (i.e. latitude and longitude) and vice versa.
MIT License
207 stars 35 forks source link

City translation not working, only country works? #43

Closed udrea93 closed 1 year ago

udrea93 commented 3 years ago

Hello, im using the following code ` Geocode.setLanguage("en"); Geocode.setRegion("en"); Geocode.setLocationType = 'ROOFTOP';

  Geocode.fromLatLng("48.13512530", "11.58198050").then(
    response => {
      if (response.results[0].formatted_address) {
        console.log(response.results[0].formatted_address);

        const formattedAddressArray =
          response.results[0].formatted_address.split(' ');

        const getCity =
          formattedAddressArray[formattedAddressArray.length - 2];
        const getCountry =
          formattedAddressArray[formattedAddressArray.length - 1];
        console.log(getCity + " " + getCountry)
      }
    error => {
      setLocation(eventData.location);
      console.error(error);
    }
  );
}`

I get München, Germany I should get Munich, Germany The country seems to be translated but what about the city? Thanks!

shukerullah commented 1 year ago

Hi there,

Regarding the city name translation, the API displays results in the specified language if available; otherwise, it uses the language of the original input.

Source: Stack Overflow

Feel free to ask if you have more questions!

import {
  setKey,
  fromLatLng,
  setLanguage,
  setRegion,
  setLocationType,
} from 'react-geocode';

setKey('AIzaSyDUAzKPR3MoFHwZz7wEYThVptNvnq_cBJY');
setLanguage('en');
setRegion('en');
setLocationType('ROOFTOP');

fromLatLng('48.13512530', '11.58198050')
  .then(response => {
    if (response.results[0].formatted_address) {
      console.log(response.results[0].formatted_address);

      const formattedAddressArray =
        response.results[0].formatted_address.split(' ');

      const getCity = formattedAddressArray[formattedAddressArray.length - 2];
      const getCountry =
        formattedAddressArray[formattedAddressArray.length - 1];
      console.log(getCity + ' ' + getCountry);
    }
  })
  .catch(console.error);