Reputation: 325
I followed a tutorial to use google maps API to get last known location or request location updates if there were no such location.
Here is my 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 the first conditional is supposed to request a new location. Where is this location stored? I thought it might execute handeNewLocation (the code is 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 to get the location and store it as a string, so my problem is I don't know where exactly
LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
is saving the new location.
Here is the 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>
Upvotes: 2
Views: 75
Reputation: 43322
The call to request location updates does just that, it requests location updates.
After you make that call, new locations will come in to onLocationChanged()
If you want to always have the current location saved, you can do so with member variables.
Note that there is no need to log a null location as you are doing now, so remove the log entry in the case that location is 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 the 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 as member variable
double currentLongitude; //added as 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");
}
}
Upvotes: 1
Reputation: 1398
You need to call
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
Log.v(TAG, "old location" + location);
before you call
Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
then the location will be stored in location
.
Upvotes: 1