google maps - How to get address from latitude and longitude in Android? -


i building android application using touchablewrapper class getting latitude & longitude.when user removes finger, camera center position latitude , longitude parsed , shown in toast. need address @ latitude , longitude. here code using latitude , longitude:

public class mainactivity extends fragmentactivity implements touchactiondown, touchactionup {     cameraposition mdowncameraposition;     cameraposition mupcameraposition;     imageview submitbtn,mappoint;     string addressfixed,completed;     edittext whitebord;     @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_maintut);          // data views         mappoint = (imageview) findviewbyid(r.id.mappoint);         whitebord = (edittext) findviewbyid(r.id.searchmapedit);         mappoint.setimageresource(r.drawable.point);         submitbtn.setonclicklistener(new onclicklistener() {              @override             public void onclick(view v) {                 // todo auto-generated method stub                 onbackpressed();             }         });         getmap().getmap().setmylocationenabled(true);         getmap().getmap().setonmaploadedcallback(                 new googlemap.onmaploadedcallback() {                     @override                     public void onmaploaded() {                         location mylocation = getmap().getmap().getmylocation();                         latlng mylatlng = new latlng(mylocation.getlatitude(),                                 mylocation.getlongitude());                          cameraposition myposition = new cameraposition.builder()                                 .target(mylatlng).zoom(17).bearing(90).tilt(30)                                 .build();                         getmap().getmap().animatecamera(                                 cameraupdatefactory                                         .newcameraposition(myposition));                     }                 });     }      @override     protected void onresume() {         super.onresume();         // check google play services         int isavailable = googleplayservicesutil                 .isgoogleplayservicesavailable(this);         if (isavailable != connectionresult.success) {             googleplayservicesutil.geterrordialog(isavailable, this, 1).show();         }     }      @override     public void ontouchdown(motionevent event) {         mdowncameraposition = getmap().getmap().getcameraposition();     }          @override     public void ontouchup(motionevent event) {     mupcameraposition = getmap().getmap().getcameraposition();     getmap().getmap().clear();// remove previous marker     markeroptions options = new markeroptions()             .title("this selected place host game")             .position(                     new latlng(mupcameraposition.target.latitude,                             mupcameraposition.target.longitude));     new getaddresstask(getapplicationcontext()).execute();  } private supportmapfragment getmap() {     return ((supportmapfragment) getsupportfragmentmanager()             .findfragmentbyid(r.id.map)); }  public class getaddresstask extends asynctask<android.location.location, void, string> {      public getaddresstask (context context) {         super();         mcontext = context;     }      @override     protected string doinbackground (android.location.location... params) {         geocoder geocoder =                 new geocoder(mcontext, locale.getdefault());         android.location.location location = params[0];         location markerlocation = getmap().getmap().getmylocation();          list<address> addresses = null;         try {             if (mbymap && markerlocation != null) {                 addresses = geocoder.getfromlocation(markerlocation.getlatitude(),                         markerlocation.getlongitude(), 1);             } else if (!mbymap && location != null) {                 addresses = geocoder.getfromlocation(mupcameraposition.target.latitude,                         mupcameraposition.target.longitude, 1);             }         } catch (ioexception exception) {             log.e("complaintlocation",                     "io exception in getfromlocation()", exception);   //                handler.post(new runnable() {   //   //                    @override   //                    public void run() {  //                     toast.maketext(mcontext, //                              mcontext.getstring("updating location failed"), //                              toast.length_short).show();  //                 } //              });             return ("io exception trying address");         } catch (illegalargumentexception exception) {             string errorstring = "illegal arguments " +                     double.tostring(location.getlatitude()) + " , " +                     double.tostring(location.getlongitude()) + " passed address service";             log.e("locationsampleactivity", errorstring, exception);              return errorstring;         }          if (addresses != null && addresses.size() > 0) {             address address = addresses.get(0);              if (address.getmaxaddresslineindex() > 0) {                 return string.format(                         "%s/%s/%s/%s/%s/%s",                         address.getlatitude(), // 0                         address.getlongitude(), // 1                         address.getthoroughfare(), // 2                         address.getsubthoroughfare(), //3                         address.getpostalcode(), // 4                         address.getlocality()); // 5             } else {                 return string.format(                         "%s/%s/%s/%s",                         address.getlatitude(), // 0                         address.getlongitude(), // 1                         address.getpostalcode(), // 2                         address.getlocality()); // 3             }         } else return "no address found";     }      // format address string after lookup     @override     protected void onpostexecute (string address) {          string[] addressfields = textutils.split(address, "/");         log.d("address array", arrays.tostring(addressfields));   //            log.d("location", "using " + mprovider);          // workaround: doinbackground can return strings instead of, example,         // address instance or string[] directly. able use textutils.isempty()         // on fields returned method, set each string reads "null"         // null reference         (int fieldcnt = 0; fieldcnt < addressfields.length; ++fieldcnt) {             if (addressfields[fieldcnt].equals("null"))                 addressfields[fieldcnt] = null;         }          string mstreet,mhousenumber,mlatitude,mlongtitude,mpostalcode,mcity;         switch (addressfields.length) {             case 4:                 mstreet = null;                 mhousenumber = null;                 mlatitude = addressfields[0];                 mlongtitude = addressfields[1];                 mpostalcode = addressfields[2];                 mcity = addressfields[3];                 break;             case 6:                 mlatitude = addressfields[0];                 mlongtitude = addressfields[1];                 mstreet = addressfields[2];                 mhousenumber = addressfields[3];                 mpostalcode = addressfields[4];                 mcity = addressfields[5];                 break;             default:                 mlatitude = null;                 mlongtitude = null;                 mstreet = null;                 mhousenumber = null;                 mpostalcode = null;                 mcity = null;                 break;         }         toast.maketext(getapplicationcontext(), mstreet,                    toast.length_long).show();     } }   private boolean mbymap;  // lookup address via reverse geolocation public void lookupaddress (boolean bymap) {     mbymap = bymap;     if (geocoder.ispresent()) { //          (new getaddresstask(mcontext)).execute(mcurrentbestlocation);     } }      private supportmapfragment getmap() {         return ((supportmapfragment) getsupportfragmentmanager()                 .findfragmentbyid(r.id.map));     } } 

any appreciated.

you can address geocoder object note receive list of suggested addresses. here in example take first one

geocoder geocoder; list<address> youraddresses; geocoder = new geocoder(this, locale.getdefault()); youraddresses= geocoder.getfromlocation(yourlatitude, yourlongitude, 1);  if (youraddress.size() > 0) {  string youraddress = youraddresses.get(0).getaddressline(0);  string yourcity = youraddresses.get(0).getaddressline(1);  string yourcountry = youraddresses.get(0).getaddressline(2); } 

Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -