Accessing Latitude and Longitude Android Google API -


i'm making app able access , use user's latitude , longitude display nearby restaurants.

i trying retrieve latitude , longitude variables onconnected(bundle bunle) method, shown below, access , use them in oncreate() method, string latitude declared @ top wont store latitude value when set equal users latitude on onconnnected() method. tried test result when displayed string on toast message, toast blank, indicating string had not been set equal latitude value. however, if try displaying toast within scope of onconnected() method, works. need access latitude , longitude outside of onconnected() method, in oncreate() method.

i have posted code below! appreciated!

    public class mainactivity extends appcompatactivity implements googleapiclient.onconnectionfailedlistener, googleapiclient.connectioncallbacks, com.google.android.gms.location.locationlistener {  protected static final string tag = "gokul log message: "; protected googleapiclient mgoogleapiclient; protected location mlastlocation; protected textview textlatitude; public string mlat = null;  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);      textlatitude = (textview)this.findviewbyid(r.id.textlatitude);      toast.maketext(mainactivity.this, mlat, toast.length_long).show();      buildgoogleapiclient();  }  protected synchronized void buildgoogleapiclient(){     log.v("log1", "entering buildgoogleapiclient method");      mgoogleapiclient = new googleapiclient.builder(this)             .addconnectioncallbacks(this)             .addonconnectionfailedlistener(this)             .addapi(locationservices.api)             .build(); } @override protected void onstart(){     log.v("log1", "entering onstart() method");      super.onstart();     mgoogleapiclient.connect(); } @override protected void onstop(){     log.v("log1", "entering onstop() method");      super.onstop();     if (mgoogleapiclient.isconnected()){         mgoogleapiclient.disconnect();     } } @override public boolean oncreateoptionsmenu(menu menu) {     // inflate menu; adds items action bar if present.     getmenuinflater().inflate(r.menu.menu_main, menu);     return true;  }  @override public boolean onoptionsitemselected(menuitem item) {     // handle action bar item clicks here. action bar     // automatically handle clicks on home/up button, long     // specify parent activity in androidmanifest.xml.     int id = item.getitemid();      //noinspection simplifiableifstatement     if (id == r.id.action_settings) {         return true;     }      return super.onoptionsitemselected(item); }  @override public void onconnected(bundle bundle) {     log.v("log1", "entering onconnected() method");       mlastlocation = locationservices.fusedlocationapi.getlastlocation(mgoogleapiclient);     if (mlastlocation != null){         textlatitude.settext(string.valueof(mlastlocation.getlatitude()));        //textlongitude.settext(string.valueof(mlastlocation.getlongitude()));         mlat = string.valueof(mlastlocation.getlatitude());         log.v("log1onconnected", mlat + "asd");     }      log.v("log1onconnected2", mlat + "asd");  }  @override public void onconnectionsuspended(int i) {       log.i(tag, "connection suspended");     mgoogleapiclient.connect();  }  @override public void onlocationchanged(location location) {  }  @override public void onconnectionfailed(connectionresult connectionresult) {      log.i(tag, "connection failed: " + connectionresult.geterrorcode());  } 

}

it doesn't changed because doesn't have location yet. because connect google play doesn't mean location ready. need listen location updates , set value in onlocationchanged.


Comments