I am trying a simple app where i need to display a map.And when i click on any location it should return the address of that location.Can u please guide me as how to proceed with this.
Asked
Active
Viewed 2,518 times
3 Answers
6
I Found Google API is very handy for reverse Geo coding. For this I've created this class. You can directly use this class in your app :
public class getReverseGeoCoding
{
private String Address1 = "", Address2 = "", City = "", State = "", Country = "", County = "", PIN = "";
public void getAddress()
{
Address1 = ""; Address2 = ""; City = ""; State = ""; Country = ""; County = ""; PIN = "";
try {
JSONObject jsonObj = parser_Json.getJSONfromURL("http://maps.googleapis.com/maps/api/geocode/json?latlng="+ Global.curLatitude + "," + Global.curLongitude +"&sensor=true");
String Status = jsonObj.getString("status");
if(Status.equalsIgnoreCase("OK"))
{
JSONArray Results = jsonObj.getJSONArray("results");
JSONObject zero = Results.getJSONObject(0);
JSONArray address_components = zero.getJSONArray("address_components");
for(int i = 0; i<address_components.length(); i++)
{
JSONObject zero2 = address_components.getJSONObject(i);
String long_name = zero2.getString("long_name");
JSONArray mtypes = zero2.getJSONArray("types");
String Type = mtypes.getString(0);
if(TextUtils.isEmpty(long_name) == false || !long_name.equals(null) || long_name.length() > 0 || long_name != "" )
{
if(Type.equalsIgnoreCase("street_number"))
{
Address1 = long_name + " ";
}
else if(Type.equalsIgnoreCase("route"))
{
Address1 = Address1 + long_name;
}
else if(Type.equalsIgnoreCase("sublocality"))
{
Address2 = long_name;
}
else if(Type.equalsIgnoreCase("locality"))
{
// Address2 = Address2 + long_name + ", ";
City = long_name;
}
else if(Type.equalsIgnoreCase("administrative_area_level_2"))
{
County = long_name;
}
else if(Type.equalsIgnoreCase("administrative_area_level_1"))
{
State = long_name;
}
else if(Type.equalsIgnoreCase("country"))
{
Country = long_name;
}
else if(Type.equalsIgnoreCase("postal_code"))
{
PIN = long_name;
}
}
// JSONArray mtypes = zero2.getJSONArray("types");
// String Type = mtypes.getString(0);
// Log.e(Type,long_name);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public String getAddress1()
{
return Address1;
}
public String getAddress2()
{
return Address2;
}
public String getCity()
{
return City;
}
public String getState()
{
return State;
}
public String getCountry()
{
return Country;
}
public String getCounty()
{
return County;
}
public String getPIN()
{
return PIN;
}

Vipul Purohit
- 9,807
- 6
- 53
- 76
0
Do you know how to display a map in your android app? If not you can take a look at this nice tutorial.
Then displaying address can be done like this:
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);

orchidrudra
- 1,172
- 1
- 12
- 30
0
for getting the coordinate of a click, check this thread:
Getting coordinates when clicking anywhere on a MapView
and you can turn a coordinate into an adress with this:
Address a = ((Address)geocoder.getFromLocation(latiAsDouble, longiAsDouble, 1).get(0));
String Position = a.getAddressLine(1) + " - " + a.getAddressLine(0);
this does a http call to a google server, so you might want to put this into some kind of thread.