Question: For Java Android Studio, I need to display the address given the latitude and longitude (which I obtained in the onLocationChanged method). I tried the

For Java Android Studio, I need to display the address given the latitude and longitude (which I obtained in the onLocationChanged method). I tried the following method:

---------------------------------------------------------------------------------------------------------------------------------------------

public String getAddress(Context ctx, double lat, double lng){    String fullAdd=null;    try{        Geocoder geocoder= new Geocoder(ctx, Locale.getDefault());        List addresses = geocoder.getFromLocation(lat, lng, 1);        if(addresses.size()>0){            Address address = addresses.get(0);            fullAdd = address.getAddressLine(0);         }    }catch(IOException ex){        ex.printStackTrace();    }    return fullAdd; }

---------------------------------------------------------------------------------------------------------------------------------------------------

After that, in my onLocationChanged, I set a textview's text to

getAddress(this, location.getLatitude(), location.getLongitude()); 

However, I keep getting null for the address. How do I fix this and make it diplay the address given the latitude and longitude?


Step by Step Solution

3.38 Rating (157 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

Geocoder geocoder List addresses geocoder new Geocoderthis Loc... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Accounting Questions!