How to get the city name by latitude and longitude using openstreetmap in android

android, openstreetmap, osmdroid

Solution

This can be done through Reverse Geocoding, where you convert a Latitude/Longitude point into a human readable address. Typically, reverse geocoding is done as a web-request.

In your case, Open Street Maps has a reverse geocoding API where you can make requests to their web service and then consume the xml/json response to get the city name.

Making reverse geocode request to the osm web service would look something like this:

http://nominatim.openstreetmap.org/reverse?format=xml&lat=[LATITUDE]&lon=[LONGITUDE]&zoom=18&addressdetails=1

Where:

- [LATITUDE] is the latitude point to be reverse geocoded.

- [LONGITUDE] is the longitude point to be reverse geocoded.

This will give you a result that looks something like so:

<reversegeocode timestamp="Fri, 06 Nov 09 16:33:54 +0000" querystring="...">
   <result place_id="1620612" osm_type="node" osm_id="452010817">
     135, Pilkington Avenue, Wylde Green, City of Birmingham, West Midlands (county), B72, United Kingdom
   </result>
   <addressparts>
     <house>135</house>
     <road>Pilkington Avenue</road>
     <village>Wylde Green</village>
     <town>Sutton Coldfield</town>
     <city>City of Birmingham</city>
     <county>West Midlands (county)</county>
     <postcode>B72</postcode>
     <country>United Kingdom</country>
     <country_code>gb</country_code>
   </addressparts>   
 </reversegeocode>

You can then process the result as an xml document to find the city name node.

An alternative is to use Google's Geolocation API.

Problem

In my app i am using osm rather than google map.I have latitude and longitude.So from here how i will query to get the city name from osm database..plz help me.I am using osmdroid-android-3.0.8.is there any extra library i have to download for this?

Original source