marlove / react-native-geocoding

MIT License
208 stars 54 forks source link

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

Open FrozenIce0617 opened 4 years ago

FrozenIce0617 commented 4 years ago

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.

amit13091992 commented 4 years ago

@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.

jordanchaitin commented 3 years ago

@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?