Creating Android Location Object-ThrowExceptions

Exception or error:

I am using the Geocoder class to fetch multiple location objects by using the following code:

Geocoder geocoder = new Geocoder(this, Locale.getDefault());
   List<Address> addresses = geocoder.getFromLocation(LATITUDE, LONGITUDE, 3);
   Address[] addresses_array = new Address[addresses.size()];

    addresses.toArray(addresses_array);
    for( int i = 0; i < addresses_array.length; i++ ){
       //create location object here
       locOBJ.setLatitude(LATITUDE);
       locOBJ.setLongitude(LONGITUDE);
    }

In addition, inside the forloop, I am trying to dymanically create location objects to add to an array;

How can I create blank location objects ?

How to solve:

Assuming you refer to android.location.Location use the constructor which takes a provider string and set it to whatever you want.

###

That’s not really what it’s intended for, if you are looking to plot stuff on a google map you may want to look into the GeoPoint class. You must use the GeoPoint class when dealing with Map OverlayItem objects. What do you plan to do with the Location objects? Also you should do the getFromLocation call in a thread or AsyncTask since it is doing a remote server call.

using the GeoPoint class.

Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(LATITUDE, LONGITUDE, 3);
int size = addresses.size();
GeoPoint gp[] = new GeoPoint[size];
for(int i = 0; i<size; i++) {
   Address addr = addresses.get(i);
   gp[i] = new GeoPoint(addr.getLatitude()*1000000, 
                        address.getLongitude()*1000000);
}

The values are * 1000000 because the GeoPoint wants E6 values. Also realize that if there are no matches the array may be length 0.

Leave a Reply

Your email address will not be published. Required fields are marked *