android - The constructor Geocoder() is undefined -


i keep getting geocoder being undefined following code. trying address of place lat , long. line geocoder geocoder = new geocoder(this, locale.english); comes undifined.

@override public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.main);      /* use locationmanager class obtain gps locations */     locationmanager mlocmanager =     (locationmanager)getsystemservice(context.location_service);     locationlistener mloclistener = new mylocationlistener();     mlocmanager.requestlocationupdates(locationmanager.gps_provider, 600000, 1000, mloclistener);  /* class location listener */ public class mylocationlistener implements locationlistener { @override public void onlocationchanged(location loc) { loc.getlatitude(); loc.getlongitude(); geocoder geocoder = new geocoder(this, locale.english); currentlatitude = loc.getlatitude(); currentlongitude = loc.getlongitude();  try {       list<address> addresses = geocoder.getfromlocation(currentlatitude, currentlongitude, 1);        if(addresses != null) {        address returnedaddress = addresses.get(0);        stringbuilder strreturnedaddress = new stringbuilder("address:\n");        for(int i=0; i<returnedaddress.getmaxaddresslineindex(); i++) {         strreturnedaddress.append(returnedaddress.getaddressline(i)).append("\n");        }        myaddress.settext(strreturnedaddress.tostring());       }       else{        myaddress.settext("no address returned!");       }      } catch (ioexception e) {       // todo auto-generated catch block       e.printstacktrace();       myaddress.settext("canont address!");      } 

that's because undefined: http://developer.android.com/reference/android/location/geocoder.html shows 2 constructors. 1 takes context, other takes context , locale, crucially creating inside mylocationlistener class. try using this:

geocoder geocoder = new geocoder(<activity class name>.this, locale.english); 

instead.

for performance reasons may want create single instance of geocoder , in constructor mylocationlistener class, rather spawning 1 every time location update.


Comments