java - How to get location after updates? -
i followed tutorial use google maps api last known location or request location updates if there no such location.
here onconnected method
@override public void onconnected(bundle bundle) { location location = locationservices.fusedlocationapi.getlastlocation(mgoogleapiclient); if (location == null) { locationservices.fusedlocationapi.requestlocationupdates(mgoogleapiclient, mlocationrequest, this); log.v(tag, "old location" + location); } else { handlenewlocation(location); log.v(tag, "new location"); } }
so first conditional supposed request new location. location stored? thought might execute handenewlocation (the code below)
private void handlenewlocation(location location) { log.d(tag, location.tostring()); double currentlatitude = location.getlatitude(); double currentlongitude = location.getlongitude(); loc = "latitude: " + double.tostring(currentlatitude) + ", longitude: " + double.tostring(currentlongitude); log.v(tag, loc); }
i need location , store string, problem don't know
locationservices.fusedlocationapi.getlastlocation(mgoogleapiclient);
is saving new location.
here manifest:
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.write_external_storage" /> <uses-permission android:name="android.permission.access_fine_location" /> <uses-permission android:name="android.permission.access_coarse_location" /> <uses-permission android:name="android.permission.internet" /> <uses-permission android:name="android.permission.access_network_state" /> <uses-permission android:name="com.google.android.providers.gsf.permission.read_gservices" /> <application android:allowbackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > <activity android:name=".mainactivity" android:label="@string/app_name" > </activity> <activity android:name=".giveactivity" android:label="@string/title_activity_give" android:parentactivityname=".mainactivity" > <meta-data android:name="android.support.parent_activity" android:value="ashkanbakhshai.com.greenlight.mainactivity" /> </activity> <activity android:name=".popactivity" android:label="@string/title_activity_pop" android:theme="@style/fullscreen" > </activity> <activity android:name=".signatureactivity" android:fillviewport="true" android:label="@string/title_activity_signature" android:screenorientation="landscape" > </activity> <activity android:name=".owneractivity" android:label="@string/title_activity_owner" android:screenorientation="landscape" android:theme="@style/theme.appcompat.dialog" > </activity> <activity android:name=".takeactivity" android:label="@string/title_activity_take" android:screenorientation="portrait" > </activity> <activity android:name=".signatureownactivity" android:label="@string/title_activity_signature_own" android:screenorientation="landscape" > </activity> <activity android:name=".greenlighted" android:label="@string/title_activity_green_lighted" android:screenorientation="portrait" > </activity> <activity android:name=".gameactivity" android:label="@string/title_activity_game" > </activity> <activity android:name=".gameownactivity" android:label="@string/title_activity_game_own" > </activity> <activity android:name=".redlighted" android:label="@string/title_activity_red_lighted" android:theme="@style/givefull" > </activity> <activity android:name=".testactivity" android:label="@string/title_activity_test" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <activity android:name=".historyactivity" android:label="@string/title_activity_history" > </activity> <activity android:name=".detailhistoryactivity" android:label="@string/title_activity_detail_history" > </activity> <activity android:name=".registeractivity" android:label="@string/title_activity_register" > </activity> <activity android:name=".navigationactivity" android:configchanges="orientation|keyboardhidden|screensize" android:label="@string/title_activity_navigation" > </activity> <activity android:name=".exitactivity" android:label="@string/title_activity_exit" android:theme="@style/theme.appcompat.dialog" > </activity> <activity android:name=".exitownactivity" android:label="@string/title_activity_exit_own" android:theme="@style/theme.appcompat.dialog" > </activity> <activity android:name=".hislistactivity" android:label="@string/title_activity_his_list" > <meta-data android:name="android.support.parent_activity" android:value="ashkanbakhshai.com.greenlight.historyactivity" /> </activity> <activity android:name=".settingsactivity" android:label="@string/title_activity_settings" > </activity> <activity android:name=".gamealtactivity" android:label="@string/title_activity_game_alt" > </activity> <activity android:name=".gameownaltactivity" android:label="@string/title_activity_game_own_alt" > </activity> <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> <meta-data android:name="com.google.android.geo.aizasybhbrcicn2yqv883z8bwe3mm5-pnhugmli" android:value="aizasybhbrcicn2yqv883z8bwe3mm5-pnhugmli" /> <meta-data android:name="com.google.android.maps.v2.api_key" android:value="@string/google_maps_key" /> </application>
the call request location updates that, requests location updates.
after make call, new locations come in onlocationchanged()
if want have current location saved, can member variables.
note there no need log null location doing now, remove log entry in case location null:
location location = locationservices.fusedlocationapi.getlastlocation(mgoogleapiclient); if (location == null) { locationservices.fusedlocationapi.requestlocationupdates(mgoogleapiclient, mlocationrequest, this); } else { handlenewlocation(location); log.v(tag, "new location"); }
then update member variables in handlenewlocation()
:
private void handlenewlocation(location location) { log.d(tag, location.tostring()); currentlatitude = location.getlatitude(); //update member variable currentlongitude = location.getlongitude(); //update member variable loc = "latitude: " + double.tostring(currentlatitude) + ", longitude: " + double.tostring(currentlongitude); log.v(tag, loc); }
then, call handlenewlocation()
in onlocationchanged()
callback:
@override public void onlocationchanged(location location) { //location changed, update current location handlenewlocation(location); log.v(tag, "new location"); }
full class:
public class locationactivity extends activity implements googleapiclient.connectioncallbacks, googleapiclient.onconnectionfailedlistener, locationlistener { locationrequest mlocationrequest; googleapiclient mgoogleapiclient; double currentlatitude; //added member variable double currentlongitude; //added member variable string tag = "myapp"; string loc; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); buildgoogleapiclient(); mgoogleapiclient.connect(); } @override protected void onpause(){ super.onpause(); if (mgoogleapiclient != null) { locationservices.fusedlocationapi.removelocationupdates(mgoogleapiclient, this); } } protected synchronized void buildgoogleapiclient() { toast.maketext(this,"buildgoogleapiclient",toast.length_short).show(); mgoogleapiclient = new googleapiclient.builder(this) .addconnectioncallbacks(this) .addonconnectionfailedlistener(this) .addapi(locationservices.api) .build(); } @override public void onconnected(bundle bundle) { mlocationrequest = new locationrequest(); mlocationrequest.setinterval(10); mlocationrequest.setfastestinterval(10); mlocationrequest.setpriority(locationrequest.priority_balanced_power_accuracy); //mlocationrequest.setsmallestdisplacement(0.1f); location location = locationservices.fusedlocationapi.getlastlocation(mgoogleapiclient); if (location == null) { locationservices.fusedlocationapi.requestlocationupdates(mgoogleapiclient, mlocationrequest, this); } else { handlenewlocation(location); log.v(tag, "new location"); } } private void handlenewlocation(location location) { log.d(tag, location.tostring()); currentlatitude = location.getlatitude(); //update member variable currentlongitude = location.getlongitude(); //update member variable loc = "latitude: " + double.tostring(currentlatitude) + ", longitude: " + double.tostring(currentlongitude); log.v(tag, loc); } @override public void onconnectionsuspended(int i) { toast.maketext(this,"onconnectionsuspended",toast.length_short).show(); } @override public void onconnectionfailed(connectionresult connectionresult) { toast.maketext(this,"onconnectionfailed",toast.length_short).show(); } @override public void onlocationchanged(location location) { //location changed, update current location handlenewlocation(location); log.v(tag, "new location"); } }
Comments
Post a Comment