Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get State name from City name #29

Open
FrozenIce0617 opened this issue Jan 29, 2020 · 2 comments
Open

How to get State name from City name #29

FrozenIce0617 opened this issue Jan 29, 2020 · 2 comments

Comments

@FrozenIce0617
Copy link

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
Copy link

@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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants