ez4nick
ez4nick

Reputation: 10199

getting address from latitude and longitude

I am trying to get the address of a user from the latitude and longitude like this:

LocationManager locationManager = (LocationManager)  
this.getSystemService(Context.LOCATION_SERVICE);
    // Define a listener that responds to location updates
    final LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
            // Called when a new location is found by the network location provider.

            lat =location.getLatitude();
            lon = location.getLongitude();


        }

        public void onStatusChanged(String provider, int status, Bundle extras) {}

        public void onProviderEnabled(String provider) {}

        public void onProviderDisabled(String provider) {}
    };




    try{
        Geocoder geocoder;

        geocoder = new Geocoder(this, Locale.getDefault());
        addresses=(geocoder.getFromLocation(lat, lon, 1));

    }catch(IOException e){
        e.printStackTrace();
    }

    String address = addresses.get(0).getAddressLine(0);
    String city = addresses.get(0).getAddressLine(1);
    String country = addresses.get(0).getAddressLine(2);


    TextView tv = (TextView) findViewById(R.id.tv3);
    tv.setText(address+"\n"+city+"\n"+country);

// Register the listener with the Location Manager to receive location updates
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}

And every time I run this I get the error "IndexOutOfBoundsException: Invalid index 0, size is 0" on the line that says "String address = addresses.get(0).getAddressLine(0);". I understand the error means that I am trying to access something that isn't there but I'm not sure what is causing this. I have been searching to find the best way to get an address and this is what I found but it may not be the most efficient or best way. any suggestions?

Upvotes: 2

Views: 2104

Answers (2)

Evgeny Tanhilevich
Evgeny Tanhilevich

Reputation: 1194

As per Android docs

A class for handling geocoding and reverse geocoding. Geocoding is the process of transforming a street address or other description of a location into a (latitude, longitude) coordinate. Reverse geocoding is the process of transforming a (latitude, longitude) coordinate into a (partial) address. The amount of detail in a reverse geocoded location description may vary, for example one might contain the full street address of the closest building, while another might contain only a city name and postal code. The Geocoder class requires a backend service that is not included in the core android framework. The Geocoder query methods will return an empty list if there no backend service in the platform. Use the isPresent() method to determine whether a Geocoder implementation exists.

You are not testing Geocoder.isPresent() in your code. Most likely it is just not implemented on your device, and is just returning you an empty list. I have never used a Geocoder, but I can imagine that an empty list may be returned even if it is present, say you are specifying a location in the middle of the ocean. You should always test the size of the list before accessing its contents:

TextView tv = (TextView) findViewById(R.id.tv3);

if (addresses.size() > 0) {
    String address = addresses.get(0).getAddressLine(0);
    String city = addresses.get(0).getAddressLine(1);
    String country = addresses.get(0).getAddressLine(2);
    tv.setText(address+"\n"+city+"\n"+country);
} else {
    tv.setText("Oops...");
}

Upvotes: 2

Ryan S
Ryan S

Reputation: 4567

Lat and lon are only being set to their respective values in onLocationchanged which fires after your geocoding, try either setting the lat and lon before or put the geocoding inside onLocationChanged. I can't see all the code, but I'm guessing you're intializing lat and lon, to 0 and the geocoder has no address for 0,0

Upvotes: 3

Related Questions