Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting street name from Address/Location object in Android

I'm trying to get the street name of my current location but I can't seem to get it.

I use this method to retrieve the Address:

public Address getAddressForLocation(Context context, Location location) throws IOException {

        if (location == null) {
            return null;
        }
        double latitude = location.getLatitude();
        double longitude = location.getLongitude();
        int maxResults = 1;

        Geocoder gc = new Geocoder(context, Locale.getDefault());
        List<Address> addresses = gc.getFromLocation(latitude, longitude, maxResults);

        if (addresses.size() == 1) {
            return addresses.get(0);
        } else {
            return null;
        }
    }

And then I can do things like. address.getLocality() and address.getPostalCode()

But what I want is the street name. Like in "Potterstreet 12". When I print the AddressLine(0) and AddressLine(1) I only get the postalcode, city and country.

How can I retrieve the street name of the position i'm currently at?

like image 897
Galip Avatar asked Nov 23 '10 14:11

Galip


People also ask

How do I find a street name from latitude and longitude?

Use getLocality() and getCountryName() instead. @Shubh - Try this url - "http://maps.googleapis.com/maps/api/geocode/json?latlng=" + latitude + ","+ longitude + "&sensor=true" . It will return Json response. You don't need to specify Locale.

How do I find the geocode of a location?

Open up the text message from the share. Click on the Google Maps link. This will appear in the message after the location's address and will begin with "goo.gl/maps". Find the latitude and longitude.

What is geocoding and reverse geocoding in Android?

Geocoding is the process of converting addresses (like a street address) into geographic coordinates (like latitude and longitude), which you can use to place markers on a map, or position the map. Reverse geocoding is the process of converting geographic coordinates into a human-readable address.

Which method is used to get an address for a given latitude and longitude in Android?

getFromLocation() method to get an address for a given latitude and longitude. This method is synchronous, and may take a long time to do its work, so you should call the method from the doInBackground() method of an AsyncTask class.


1 Answers

Have you tried using getAddressLine ? See here for more info on this method

Something like this should do (untested):

for (int i = 0; i < addresses.getMaxAddressLineIndex(); i++) {
 Log.d("=Adress=",addresses.getAddressLine(i));
}
like image 99
ccheneson Avatar answered Oct 22 '22 00:10

ccheneson