android - how to get full address from passing Latitude and Latitude? -


i used code on phone bellow address { country, street, city}, didnt work many inputs, why? , crashing. please how full address passing longitude , latitude method returns available address longitude , latitude. can please provide me answer best result. me.

 import java.io.ioexception; import java.util.list; import java.util.locale; import android.app.activity; import android.content.context; import android.location.address; import android.location.geocoder; import android.location.location; import android.net.connectivitymanager; import android.net.networkinfo; import android.os.bundle; import android.os.handler; import android.util.log; import android.view.view; import android.view.view.onclicklistener;  import android.widget.button; import android.widget.edittext; import android.widget.textview;  public class get_location_name extends activity implements onclicklistener { private edittext et1; private edittext et2; private textview tv1; private button b1; static string result =""; @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);         setcontentview(r.layout.location_name);     et1=(edittext)this.findviewbyid(r.id.et1_location_name);     et2=(edittext)this.findviewbyid(r.id.et2_location_name);     tv1=(textview)this.findviewbyid(r.id.tv1_location_name);     b1=(button)this.findviewbyid(r.id.b1_location_name);     b1.setonclicklistener(this); }  @override public void onclick(view arg0) {      string s=null;     if(!et1.gettext().tostring().isempty() && !et2.gettext().tostring().isempty())     {             if(this.isonline())             {                  for(int i=0;i<=10;i++)                 {                     s=getaddressfromlocation(double.parsedouble(et2.gettext().tostring()),                     double.parsedouble(et1.gettext().tostring()),this);                 }             if(s!=null)                 {                 log.d("ssss","s"+s);                 tv1.settext(s);                 }             else                 tv1.settext("s null");             }             else                 tv1.settext("no internet connection");      }     else         tv1.settext("enter lat. , lon.");    }  public boolean isonline() {    // code check connectivity // works fine(no problems) } 

here method want modify

public static string getaddressfromlocation(final double lon,final double lat, final context context)  {        thread thread = new thread() {    @override public void run()      {         geocoder geocoder = new geocoder(context, locale.getdefault());            try {            list<address> list = geocoder.getfromlocation(                    lat, lon, 1);            if (list != null && list.size() > 0)                 {                address address = list.get(0);                result = " "+address.getaddressline(0) + ", " + address.getlocality()+","+address.getcountryname();                    }            }          catch (ioexception e)            {            log.e("fafvsafagag", "impossible connect geocoder", e);            }      }  };  thread.start();  return result;   }    } 

please answer question.

there known issue geocoder doesn't always returns value. see geocoder doesn't return value , geocoder.getfromlocationname returns null. can try send request 3 times in loop. should able return atleast once. if not then, might connection issue or can other issues server did not reply request. me never returned if connected internet. then, used this more reliable way address everytime:

//lat, lng double variables  containing latitude , longitude values.  public jsonobject getlocationinfo() {         //http request         httpget httpget = new httpget("http://maps.google.com/maps/api/geocode/json?latlng="+lat+","+lng+"&sensor=true");         httpclient client = new defaulthttpclient();         httpresponse response;         stringbuilder stringbuilder = new stringbuilder();          try {             response = client.execute(httpget);             httpentity entity = response.getentity();             inputstream stream = entity.getcontent();             int b;             while ((b = stream.read()) != -1) {                 stringbuilder.append((char) b);             }         } catch (clientprotocolexception e) {             } catch (ioexception e) {         }                 //create json string return.         jsonobject jsonobject = new jsonobject();         try {             jsonobject = new jsonobject(stringbuilder.tostring());         } catch (jsonexception e) {             e.printstacktrace();         }         return jsonobject;     } 

i called function follows complete address:

jsonobject ret = getlocationinfo(); //get json returned api call jsonobject location; string location_string; //parse value corresponding `formatted_address` key.  try {     location = ret.getjsonarray("results").getjsonobject(0);     location_string = location.getstring("formatted_address");     log.d("test", "formattted address:" + location_string); } catch (jsonexception e1) {     e1.printstacktrace();  } 

you can call inside asynctask or new thread. used asynctask same. hope helps.this worked me. if replace url lat , longitude coordinates , see returned json object in web browser. you'll see happened.


Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -