Johon Alimov
Johon Alimov

Reputation: 3

How to get Id number or code of particular city?

when user chooses particular city from spinner, maps will be opened, so user can choose particular place from the chosen city. If user goes outside of the chosen city, there will be a toast like “not your city”. I need unique id number of the city which is shown on map and to compare with city selected on on spinner

Upvotes: 0

Views: 647

Answers (2)

xomena
xomena

Reputation: 32138

You can figure out what is the place ID for the city using the reverse geocoding request for coordinates inside the city and specifying the result type equals to locality.

For example, the coordinate 55.710424,37.682381 is located inside Moscow, so the reverse geocoding with result type

https://maps.googleapis.com/maps/api/geocode/json?latlng=55.710424%2C37.682381&result_type=locality&key=YOUR_API_KEY

will return the following response

{
"results":[
  {
    "address_components":[
      {
        "long_name":"Moscow",
        "short_name":"Moscow",
        "types":[
          "locality","political"
        ]
      },
      {
        "long_name":"Moskva",
        "short_name":"Moskva",
        "types":[
          "administrative_area_level_2","political"
        ]
      },
      {
        "long_name":"Moscow",
        "short_name":"Moscow",
        "types":[
          "administrative_area_level_1","political"
        ]
      },
      {
        "long_name":"Russia",
        "short_name":"RU",
        "types":[
          "country","political"
        ]
      }
    ],
    "formatted_address":"Moscow, Russia",
    "geometry":{
      "bounds":{
        "northeast":{
          "lat":56.009657,"lng":37.9456611
        },
        "southwest":{
          "lat":55.48992699999999,"lng":37.3193289
        }
      },
      "location":{
        "lat":55.755826,"lng":37.6172999
      },
      "location_type":"APPROXIMATE",
      "viewport":{
        "northeast":{
          "lat":56.009657,"lng":37.9456611
        },
        "southwest":{
          "lat":55.48992699999999,"lng":37.3193289
        }
      }
    },
    "place_id":"ChIJybDUc_xKtUYRTM9XV8zWRD0",
    "types":[
      "locality","political"
    ]
  }
],
"status":"OK"
}

and you can see that the place ID of Moscow is ChIJybDUc_xKtUYRTM9XV8zWRD0.

enter image description here

You can use this place ID in conjunction to reverse geocoding with locality result type to check if coordinate is located inside the given city.

For example, coordinate 55.586107,38.229675 is outside of Moscow boundaries and reverse geocoding

https://maps.googleapis.com/maps/api/geocode/json?latlng=55.586107%2C38.229675&language=en&result_type=locality&key=YOUR_API_KEY

will give you a different locality with place ID ChIJu_L0awrASkER7yCl6FLZFLE which is a city 'Ramenskoye, Moscow Oblast, Russia'.

enter image description here

I hope you find this information useful.

Upvotes: 0

ubadub
ubadub

Reputation: 3880

Unfortunately, Google Maps API does not release information on city bounds (i.e. the polygon defining the boundaries of the city). I would suggest using OpenStreetMap, which does release this data, and has unique ID numbers for any city/region/country/etc. Here is a search engine built using OSM, if you look at the URL, you can see there is a unique ID associated with the city (in this case, New York). And here is the wiki/documentation on how to use OSM with Android.

EDIT: Also see here

Upvotes: 1

Related Questions