Reputation: 37
I'm making a weather app and everything's working except for getting my user's location. I'm trying to do so with the play services API and calling getLongitude() and getLatitude(). However none of the code I'm writing is working because none of what I listed in the title is being run. Here's the code having to do with getting the location:
public class MainActivity extends ActionBarActivity implements GoogleApiClient.OnConnectionFailedListener, GoogleApiClient.ConnectionCallbacks {
public static final String TAG = MainActivity.class.getSimpleName();
private CurrentWeather mCurrentWeather;
private GoogleApiClient mGoogleApiClient;
private double mLongitude;
private double mLatitude;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mGoogleApiClient.connect();
}
@Override
public void onConnected(Bundle bundle) {
Log.i(TAG, "Location services connected.");
Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
mLatitude = location.getLatitude();
mLongitude = location.getLongitude();
}
@Override
public void onConnectionSuspended(int i) {
Log.i(TAG, "Location services suspended. Please reconnect.");
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.i(TAG, "Location services failed.");
}
@Override
protected void onResume() {
super.onResume();
mGoogleApiClient.connect();
}
@Override
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
}
Upvotes: 1
Views: 355
Reputation: 33238
You missed initialize GoogleApiClient
Object..
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mGoogleApiClient.connect();
Please add above code and check.
Use this library in gradle. com.google.android.gms:play-services:8.4.0
Refer below link for getLocation.
Fused location provider example
Upvotes: 1
Reputation: 2737
The location returned inside onConnected() may be NULL. So u need to check that inside onConnected()
Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (location != null) {
mLatitude = location.getLatitude();
mLongitude = location.getLongitude();
}
Now it won't get location update again so you need to request location update as below -
create a location request object
protected void createLocationRequest() {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(UPDATE_INTERVAL);
mLocationRequest.setFastestInterval(FATEST_INTERVAL);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
You can call above method in onCreate() itself.
Now Add below code line to get the location updates in onConnected() method -
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
You can stop location updates once you get the user location by below code.
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
Upvotes: 0