-2

I am using google Geocoding API to store the cities in the DB on demand.I provided an input field for the user to enter their city.I used googles places autocomplete for that.

I would like to store the city and its country what ever enter ed in the input field. Calling google geocoding API with the with data from input field, returned data in JSON.

I looked into address_components got country in it. Then I took object in address_components where types = locality. The problem is,For some locations typed in the input field, The JSON from google geocoding API doesn't have types = locality. So in these cases, which one among the objects in address_components should I store as city in my DB.
this website is exampale of google geocoding api https://findcity.in/

Need suggestions. The question may not be clear earlier. So I edited it. Thanks

saiyan
  • 512
  • 6
  • 21
  • I would suggest to filter the autocomplete API call to retrieve only cities (with `types=(cities)`). That way you can make sure you'll receive either a `locality` or an `administrative_area_level_3` in the `types` response. More info [here](https://developers.google.com/maps/documentation/places/web-service/autocomplete#place_types). You can then use the `placeId` received to query the Geocoding API, to make sure you're referring to exactly the same place. – maganap Jun 08 '21 at 17:35

2 Answers2

4

You are right, the type locality is not always present in the address components array. Depending on the country or even regions of the same country the city name might be found in different address components. This depends on the data modelling for certain area and address templates that uses Google. Unfortunately, there is no documentation for this on per country basis.

In the official documentation you can find the following explanation

The selection of specific address components in this example is based on a typical address format. You may need to select different components to align with the postal address formats of different countries. For example, the code selects the locality component, which often represents the city part of the address. However, note the following examples of how the components may differ:

  • In the UK and Sweden the correct component to display the city is postal_town.

  • In Japan, the component differs across prefectures.

  • Brooklyn and other parts of New York City do not include the city as part of the address. They use sublocality_level_1 instead.

source: https://developers.google.com/maps/documentation/javascript/examples/places-autocomplete-addressform

I hope this helps a bit.

Community
  • 1
  • 1
xomena
  • 31,125
  • 6
  • 88
  • 117
1

administrative_area_3 indicates a third-order civil entity below the country level.

An example of this would be a search query for Montgomery returning Montgomery County in your specific state before Montgomery, AL. This is partially due to region biasing returning specified results based on your current location.

Here is some documentation from Google Which goes over the GeocoderResult.

I'd like to encourage you to make sure to take a look at the Google Maps APIs reference guide before asking a question directly. It is rather robust and you can find a lot of useful information there.

Cole Huffman
  • 300
  • 2
  • 12
  • I already read the documentation. I need suggestion to consider some thing as city. In case of some special locations (Montgomery in your case) . thanks – saiyan Apr 03 '18 at 15:34