marlove/react-native-geocoding

How to get `State` name from `City` name

FrozenIce0617 opened this issue · 2 comments

Is it possible to get the state name from city name?
If then, how to get the state name with city name.

Any help would be appreciated.

Thanks.

@FrozenIce0617
For this you need to use reverse geocoding.
example:

fetch('https://maps.googleapis.com/maps/api/geocode/json?address=' + position.latitude + ',' + position.longitude + '&key=' + 'YOUR_API_KEY')
            .then((response) => response.json())
            .then((responseJson) => {
                        streetAddress: responseJson.results[0].address_components[1].long_name,
                        city: responseJson.results[0].address_components[4].long_name,
                        state: responseJson.results[0].address_components[5].long_name,
                        country: responseJson.results[0].address_components[6].long_name,
                        landmark: responseJson.results[0].address_components[1].long_name,
                        postalCode: responseJson.results[0].address_components[6].long_name,
                        latitude: position.latitude,
                        longitude: position.longitude,
                        address: locationAddress
            })

The response array size will vary based on your address length. Try this solution it will help.

@FrozenIce0617
For this you need to use reverse geocoding.
example:

fetch('https://maps.googleapis.com/maps/api/geocode/json?address=' + position.latitude + ',' + position.longitude + '&key=' + 'YOUR_API_KEY')
            .then((response) => response.json())
            .then((responseJson) => {
                        streetAddress: responseJson.results[0].address_components[1].long_name,
                        city: responseJson.results[0].address_components[4].long_name,
                        state: responseJson.results[0].address_components[5].long_name,
                        country: responseJson.results[0].address_components[6].long_name,
                        landmark: responseJson.results[0].address_components[1].long_name,
                        postalCode: responseJson.results[0].address_components[6].long_name,
                        latitude: position.latitude,
                        longitude: position.longitude,
                        address: locationAddress
            })

The response array size will vary based on your address length. Try this solution it will help.

This is not universally true, is it?

The position of the zip code or city name in the array can vary. So this formula cannot be used for all cases.

Do you know of a more general way to do this?