Retrieve all localities and sub localities from given city google places API

saikiran picture saikiran · Sep 1, 2015 · Viewed 10.2k times · Source

I am looking for a web service or a Google place API kind of plugin to retrive localities,sub-localities and areas of the given city name.

have been trying my hands with google and geonames but can't able to get through it.

https://maps.googleapis.com/maps/api/place/autocomplete/json?input=hyderabad&types=(locality)&language=en_US&key=My_KEY


https://maps.googleapis.com/maps/api/geocode/json?/?address=hyderabad&key=My_KEY

have tried these two ways. any help would be appreciated.

Answer

Mahesh picture Mahesh · Dec 20, 2015

A city is defined as

locality indicates an incorporated city or town political entity.

and a sublocality as:

sublocality indicates a first-order civil entity below a locality. For some locations may receive one of the additional types: sublocality_level_1 to sublocality_level_5.

in address component types in geocoding. Also should refer the list of supported types to use with the types request parameter and those returned by the Google APIs.

The idea is to start with a designated sublocality_level_1 in your city, retrieve sublocality_level_2s, store them to DB. Repeat, but only this time with sublocality_level_2 to get the sublocality_level_3. The key is to store geometry and addressName (longname, shortname etc) in DB, so that you can run them again to get adequate, if not all information, thus simulating a zooming behavior. Finally use autocomplete APIs to retrieve a (sub)locality that don't make it to your store, but one you know should have been retrieved.

Substituting the level_1 sublocality, locality and level_1 admin areas in geocoding request json:

https://maps.googleapis.com/maps/api/geocode/xml?address=[sublocality.1],+[locality],+[admin_area_level.1]&key=API_KEY

Verify that the sublocality.1 ends up in the right place.

<address_component>
  <long_name>sublocality.1</long_name>
  <short_name>sublocality.1</short_name>
  <type>sublocality_level_1</type>
  <type>sublocality</type>
  <type>political</type>
</address_component>
<..></..>
<..></..>
<geometry>
  <location>
    <lat>nn.nnnnnnnnn</lat>
    <lng>nn.nnnnnnnnn</lng>
  </location>

Use Radar search with query parameters location=&radius=10000&types=(regions)

Refering to supported types

The (regions) type collection instructs the Places service to return any result matching the following types:

Next is extract all the place_id, use the place details API that takes place_id as a query parameter and requires a api_key.

The place details response should have all sublocality_level_2s for chosen sublocality_level_1. Store them to DB omitting duplicates by name. At the end of this, you have one location for locality (city), chosen sublocality_level_1 and number of sublocality_level_2 Location entities persisted.

This should do for sublocality_level_2. Since they occupy considerable area, missing them ideally may not happen, I believe. Should you miss, use Autocomplete APIs if you know the starting characters or Reverse Geocoding if you know the exact geometry of the missing level_2.

The above steps, repeat for sublocality_level_3 under a given level_2 of known name.

For every geometry use Reverse Geocoding to get a level_3, which if not present in the store, running against the Place details, retrieve sublocality_level_2 again and store. This is needed as some level_3 directly are associated to level_1s. This step is analogous to zooming carefully on the map to find a locality.

Use the JSON

https://maps.googleapis.com/maps/api/place/autocomplete/json?input=&types=(regions)&location=latitude,longitude&radius=1000&key=YOUR_API_KEY

Note the () around regions. That gives you the locality and others. Refer to https://developers.google.com/places/web-service/autocomplete

The whole process can be repeated with the sub_locality_type_1 adjacently co-located to the previously used one. Use nearby search (place search) for this. Query parameters for location should be the geometries of the southwest and northeast bounds of the above level_1 (sub) locality. Note to use radius=1. The JSON URI used is:

https://maps.googleapis.com/maps/api/place/nearbysearch/output?parameters