Reverse Geocoding Is Not Working
I am writting an application which needs to find the current location. The code is returning lattitude and longitude correctly but doesnt return the real address(reverse geocoding)
Solution 1:
Reverse Geocoding does not work with Emulator, test on device.
Solution 2:
public static String getUserLocation(String lat, String lon) {
String userlocation = null;
String readUserFeed = readUserLocationFeed(lat.trim() + ","+ lon.trim());
try {
JSONObject Strjson = new JSONObject(readUserFeed);
JSONArray jsonArray = new JSONArray(Strjson.getString("results"));
userlocation = jsonArray.getJSONObject(1)
.getString("formatted_address").toString();
} catch (Exception e) {
e.printStackTrace();
}
Log.i("User Location ", userlocation);
return userlocation;
}
public static String readUserLocationFeed(String address) {
StringBuilder builder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("http://maps.google.com/maps/api/geocode/json?latlng="+ address + "&sensor=false");
try {
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
} else {
Log.e(ReverseGeocode.class.toString(), "Failed to download file");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return builder.toString();
}
Post a Comment for "Reverse Geocoding Is Not Working"