19

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 ?

Robby Pond
  • 73,164
  • 16
  • 126
  • 119
sisko
  • 9,604
  • 20
  • 67
  • 139

2 Answers2

38

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

Reuben Scratton
  • 38,595
  • 9
  • 77
  • 86
  • 3
    Thank you, I didn't realized the string could be anything I choose. The documentation says "Location(String provider)" - I kept wondering what provider is supposed to be (gps, 3G ... ?) – sisko Feb 09 '11 at 14:23
  • 9
    GPS is one provider, "network" is another (which can mean wifi mac lookup or nearest cell tower). Those are the built-in system providers of Location object. Since it's your code that's providing (instantiating) these new Location objects, the provider string is entirely up to you. – Reuben Scratton Feb 09 '11 at 14:28
  • 2
    Heh, I thought it would actually validate and/or query the provider specified. Nice one, thanks. :) – DarthJDG May 24 '13 at 09:57
  • Really bad example of documentation for Android, wasted quite some time on this. My other question is suppose I provide a valid provider like gps, will it give me the current location? In other words what is the significance of it. – Utsav Gupta Oct 10 '15 at 14:02
4

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.

Robby Pond
  • 73,164
  • 16
  • 126
  • 119
  • Thanks, you are definately right about doing this in an async class. That will be one of my next moves. For now I want to collect multiple location obects in an array and assiciate each one with a map button. When a location's map button is clicked, the mapview opens up showing the location. – sisko Feb 09 '11 at 14:19
  • 1
    So then you should use the Geopoint class instead of Location. updated my answer. – Robby Pond Feb 09 '11 at 14:21
  • Excuse me if I am being silly but I just tried creating a Geopoint object but the class is not recognised. Searching this forum, I found and tried the following: GeoPoint gp = GeoPointUtils.getGeoPoint(coords[0].trim(), coords[1].trim()); ..........??? – sisko Feb 09 '11 at 14:47
  • The GeoPoint class is part of the Android Google Maps api. It's linked in my answer. – Robby Pond Feb 09 '11 at 14:50
  • Yeah, I figured it out. I am not extending OverlayItem just Activity so I unfortunately can't use GeoPoint - too bad! – sisko Feb 09 '11 at 16:57
  • @sisko huh? To display anything on the map you will need to use an Overlay and Overlay Item. That has nothing to do with the Activity. – Robby Pond Feb 09 '11 at 17:40
  • 1
    How about LatLong class @RobbyPond? http://developer.android.com/reference/com/google/android/gms/maps/model/LatLng.html – ericn Apr 28 '13 at 11:04