Reputation: 677
I am trying to get location address from Latitude and longitude in android. My code is as follow.
public String getAddress(double latitude, double longitude) {
Geocoder myLocation = new Geocoder(this, Locale.getDefault());
List<Address> myList = null;
try {
myList = myLocation.getFromLocation(latitude,longitude,1);
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String address = myList.get(0).getAddressLine(0);
return address;
}
But somehow I get null return at myList = myLocation.getFromLocation(latitude,longitude,1);
What could be wrong?
Best,
Upvotes: 0
Views: 1036
Reputation: 7092
You can use via google api like insert your longitude and latitude with parameter and send request to the server and server giving the response with address.
public static String getAddressUrl(Context context) { String responseText=null;
/*String latitude="38.89";
String longitude ="-77.03";*/
Log.v(TAG , "Latitude is: " + latitude + "Longitude is:"+ longitude);
StringBuilder sbuilder=new StringBuilder();
String googleurl="http://maps.google.com/maps/geo?"
sbuilder.append(googleurl);
sbuilder.append("q="+latitude+","+longitude);
sbuilder.append("&output=responseText&sensor=true");
String url=sbuilder.toString();
Log.v(TAG, "url is: "+url);
try {
DefaultHttpClient httpclient=new DefaultHttpClient();
HttpPost httppost=new HttpPost(url);
HttpResponse httpresponse=httpclient.execute(httppost);
HttpEntity httpentity=httpresponse.getEntity();
InputStream is=httpentity.getContent();
BufferedReader reader=new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb=new StringBuilder();
String line=null;
while((line=reader.readLine())!=null)
{
sb.append(line+"\n");
}
is.close();
responseText=sb.toString();
}
catch(ClientProtocolException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
return responseText;
}
Upvotes: 0
Reputation: 3437
You can try google reserve-geocoding if you want to find places, try google place
Upvotes: 1
Reputation: 28093
It is common.You will get Null if server does not return any value or if it is overloaded.
I faced similar issue and got the solution on following post.
Upvotes: 2
Reputation: 5472
Try, the following code.
try {
// Get the location manager
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
bestProvider = locationManager.getBestProvider(criteria, false);
Location location = locationManager
.getLastKnownLocation(bestProvider);
double lat = location.getLatitude();
double lon = location.getLongitude();
Geocoder gc = new Geocoder(this);
List<Address> lstAdd = gc.getFromLocation(lat, lon, 1);
Address ad = lstAdd.get(0);
String str = ad.getAddressLine(0);
Toast tst = Toast.makeText(this, "Your current location is " + str,
Toast.LENGTH_LONG);
tst.show();
} catch (Exception e) {
Toast tst = Toast.makeText(this,"Please check your gps settings",
Toast.LENGTH_LONG);
tst.show();
}
But, ofcourse try on real device.
Upvotes: 0