java - Can't Get address using Longitude & Latitude Android -


i trying address longitude , latitude unable address. getting longi. , lati. value when pass function of getaddress stop working kindly me if guys can. here code

mainactivity.java file

package com.example.mygps;  import java.io.ioexception; import java.util.list; import java.util.locale;  import android.app.activity; import android.location.address; import android.location.geocoder; import android.os.bundle; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.textview; import android.widget.toast;   public class mainactivity extends activity {      button btnget;     gps_class gps;     textview adr,cty,ctry;      double longi, lati;     @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);          btnget = (button)findviewbyid(r.id.btngo);         adr = (textview)findviewbyid(r.id.adr);         cty = (textview)findviewbyid(r.id.cty);         ctry = (textview)findviewbyid(r.id.ctry);          btnget.setonclicklistener(new onclicklistener() {              @override             public void onclick(view arg0) {                 // todo auto-generated method stub                 gps = new gps_class(mainactivity.this);                 if(gps.cangetlocation())                 {                      longi = gps.getlongitude();                     lati = gps.getlatitude();                     toast.maketext(mainactivity.this, "longitude is:"+longi+"latidute is:"+lati, toast.length_long).show();                     getaddress(longi, lati);                 }                 else                 {                     gps.showsettingsalert();                  }             }         });      }      public void getaddress(double longitude, double latitude)     {         double long1,lati1;         long1 = longitude;         lati1 = latitude;         if(lati>0 && long1>0)         {         geocoder geocode = new geocoder(this, locale.getdefault());         list<address> addresses;          try {             addresses = geocode.getfromlocation(latitude,longitude, 1);              string addres_ = addresses.get(0).getaddressline(0);             string country = addresses.get(0).getcountryname();             string city = addresses.get(0).getlocality();              adr.settext(addres_);             cty.settext(city);             ctry.settext(country);         } catch (ioexception e) {             // todo auto-generated catch block             e.printstacktrace();             }         }//if closing. . .          else         {             toast.maketext(this, "no vlaue", toast.length_long).show();         }       } } 

my gps_class.java file code

package com.example.mygps;  import android.app.alertdialog; import android.app.service; import android.content.context; import android.content.dialoginterface; import android.content.intent; import android.location.location; import android.location.locationlistener; import android.location.locationmanager; import android.os.bundle; import android.os.ibinder; import android.provider.settings;  public class gps_class extends service implements locationlistener{        //to context of class...     context context;      //declaring variable use. . .     double lattitude;     double longitude;      private static final long min_distance_change_for_updates = 10;     private static final long min_time_bw_updates = 1000 * 60 * 1;      boolean isgpsenabled = false;     boolean isnetworkenabled = false;     boolean cangetlocation = false;      //declaring objects of different classes...     location location;     locationmanager locationmanager;      public gps_class(context context) {         this.context = context;         getlocation();     }      //self coded function perform location works . . .     private location getlocation()     {         locationmanager = (locationmanager)context.getsystemservice(location_service);            isgpsenabled = locationmanager.isproviderenabled(locationmanager.gps_provider);          isnetworkenabled = locationmanager.isproviderenabled(locationmanager.network_provider);          if(isnetworkenabled)         {             cangetlocation = true;             locationmanager.requestlocationupdates(locationmanager.network_provider,min_time_bw_updates,min_distance_change_for_updates,this);              if(locationmanager !=null)             {                 location = locationmanager.getlastknownlocation(locationmanager.network_provider);                 if(location !=null)                 {                     lattitude = location.getlatitude();                     longitude = location.getlongitude();                 }             }         }          if(isgpsenabled)         {             cangetlocation = true;             if(location == null)             {             locationmanager.requestlocationupdates(locationmanager.gps_provider,min_time_bw_updates,min_distance_change_for_updates,this);             if(locationmanager != null)             {                 location = locationmanager.getlastknownlocation(locationmanager.gps_provider);                  if(location!=null)                 {                     lattitude = location.getlatitude();                     longitude = location.getlongitude();                 }             }         }         }          return location;     }       public void showsettingsalert(){             alertdialog.builder alertdialog = new alertdialog.builder(context);              // setting dialog title             alertdialog.settitle("gps settings");              // setting dialog message             alertdialog.setmessage("gps not enabled. want go settings menu?");              // on pressing settings button             alertdialog.setpositivebutton("settings", new dialoginterface.onclicklistener() {                 public void onclick(dialoginterface dialog,int which) {                     intent intent = new intent(settings.action_location_source_settings);                     context.startactivity(intent);                 }             });              // on pressing cancel button             alertdialog.setnegativebutton("cancel", new dialoginterface.onclicklistener() {                 public void onclick(dialoginterface dialog, int which) {                 dialog.cancel();                 }             });              // showing alert message             alertdialog.show();         }        public double getlatitude(){             if(location != null){                 lattitude = location.getlatitude();             }              // return latitude             return lattitude;         }      public double getlongitude()     {         if(location !=null)         {         longitude =location.getlongitude();         }         return longitude;     }      public boolean cangetlocation() {         return this.cangetlocation;     }     @override     public void onlocationchanged(location arg0) {         // todo auto-generated method stub      }      @override     public void onproviderdisabled(string arg0) {         // todo auto-generated method stub      }      @override     public void onproviderenabled(string arg0) {         // todo auto-generated method stub      }      @override     public void onstatuschanged(string arg0, int arg1, bundle arg2) {         // todo auto-generated method stub      }      @override     public ibinder onbind(intent arg0) {         // todo auto-generated method stub         return null;     }   } 

got solution above mention posted code fine can current location. if try run on emulator show error because "geocoder" class not compatible emulator. run on device working fine.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -