0

I want to get the location-name inside onLocationChanged(Location location)() method. We can get the Latitude and Longitude from onLocationChanged(Location location) as location.getLatitude(); and location.getLongitude(); But i need the location name. It has no method of getting location name. Can I get that?

I am doing in this way :

public void onLocationChanged(Location location) {
           double la = location.getLatitude();
           double lo= location.getLongitude();
           // location Name? 
        }
Amar
  • 855
  • 5
  • 17
  • 36

1 Answers1

0

Amar there is a method in geocoder class getFromLocation (lat,lng,maxResults) which gives a timed out exception sometimes.It has been a bug for the geocoder. So instead of calling that method just make a request asynchronously in locationChanged callback method

  @Override
public void onLocationChanged(Location location) {
       new LocationAdressAsync(this,this).execute("http://maps.googleapis.com/maps/api/geocode/json?latlng=" + location.getLongitude() + "," +  location.getLatitude()+ "&sensor=true");
}
Basheer Kohli
  • 154
  • 2
  • 11
  • I think it is not a good way of doing this. Sending a request to network for location!!! – Amar Oct 26 '16 at 12:26
  • getFromLocation (lat,lng,maxResults) which too sends a request to network for location which will not work everytime because of its timeout exception due to network error or any other cause.So in my view i think of this one.Thank you – Basheer Kohli Oct 26 '16 at 12:30
  • I am not using `geocoder` that has a method `locality` to get the location. But I believe that there will be a way here.... – Amar Oct 26 '16 at 12:33
  • @BasheerKohli what about this `LocationAdressAsync`. It not in impoting list. – Humty Oct 27 '16 at 09:15
  • its my async task class created to do background operation. – Basheer Kohli Nov 02 '16 at 05:39