views:

6445

answers:

5

I am trying to get an address based on the long/lat. it appears that something like this should work?

Geocoder myLocation = Geocoder(Locale.getDefault());
    List myList = myLocation.getFromLocation(latPoint,lngPoint,1);

The issue is that I keep getting : The method Geocoder(Locale) is undefined for the type savemaplocation

Any assistance would be helpful. Thank you.

+7  A: 

It looks like there's two things happening here.

1) You've missed the new keyword from before calling the constructor.

2) The parameter you're passing in to the Geocoder constructor is incorrect. You're passing in a Locale where it's expecting a Context.

There are two Geocoder constructors, both of which require a Context, and one also taking a Locale:

Geocoder(Context context, Locale locale)
Geocoder(Context context)

Solution

Modify your code to pass in a valid Context and include new and you should be good to go.

Geocoder myLocation = new Geocoder(getApplicationContext(), Locale.getDefault());   
List<Address> myList = myLocation.getFromLocation(latPoint, lngPoint, 1);

Note

If you're still having problems it may be a permissioning issue. Geocoding implicitly uses the Internet to perform the lookups, so your application will require an INTERNET uses-permission tag in your manifest.

Add the following uses-permission node within the manifest node of your manifest.

<uses-permission android:name="android.permission.INTERNET" />
Reto Meier
Thank you, I think I was working too late last night, not sure what happened to my new. I think I forgot it originally, put it back on when I only had my Locale, and then when I went back, forgot it again.. I appreciate it..
Chrispix
No worries, I didn't spot it first time either :)
Reto Meier
A: 

Thanks, I tried the context, locale one first, and that failed and was looking at some of the other constructors (I had seen one that had mentioned just locale). Regardless,

It did not work, as I am still getting : The method Geocoder(Context, Locale) is undefined for the type savemaplocation

I do have : import android.location.Geocoder;

Chrispix
This might be a typo, but have you missed out the 'new' keyword? I've updated my answer to explain what I mean.
Reto Meier
A: 

Well, I am still stumped. So here is more code.

Before I leave my map, I call SaveLocation(myMapView,myMapController); This is what ends up calling my geocoding information.

But since getFromLocation can throw an IOException, I had to do the following to call SaveLocation

try {
    SaveLocation(myMapView,myMapController);
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }

Then

I have to change SaveLocation by saying it throws IOExceptions : public void SaveLocation(MapView mv, MapController mc) throws IOException{

I do this : Geocoder myLocation = new Geocoder(getApplicationContext(), Locale.getDefault());
List myList = myLocation.getFromLocation(latPoint, lngPoint, 1);

And it crashes every time.

Chrispix
Could be a permissions problem. The geocoder uses the internet to do the lookups so you need an Internet uses-permission. Updated the answer with details.
Reto Meier
I have the internet permissions on there for the mapping. Very strange why it keeps failing. Let me get a logcat.
Chrispix
This is the exception I am getting : java.lang.IllegalArgumentException: latitude == 3.2945512E7
Chrispix
I think I figured it out, it needed the lat/long not E7 (i.e. 32.945512)
Chrispix
+1  A: 

The following code snippet is doing it for me (lat and lng are doubles declared above this bit):

Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);
Wilfred Knievel
I also tried this, each time I try to view the address list, it bombs out. Not sure what is going on. I will try with a new app here in a day or two to see what I can find.
Chrispix
I found this very helpful for positioning stuff:http://blogoscoped.com/archive/2008-12-15-n14.html
Wilfred Knievel
...also I've got the following two permissions in my manifest:<uses-permission android:name="android.permission.INTERNET" /><uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />When I missed one of them out (can't remember which one) the app threw a really unhelpful error.
Wilfred Knievel
A: 

I've heard that Geocoder is on the buggy side. I recently put together an example application that uses Google's http geocoding service to lookup location from lat/long. Feel free to check it out here

http://www.smnirven.com/?p=39

smnirven