The below code is an extract from my Android application to get current location I have been following http://blog.teamtreehouse.com/beginners-guide-location-android and http://droidmentor.com/get-the-current-location-in-android/ most of the documentation is outdated even the developer.google and developers.android stuff. I want to know why both functions return null and what can I do about it thank you.
@Override
public void onConnected(Bundle bundle) {
try
{
Log.d(TAG, "connection successful");
mLastLocation = LocationServices.FusedLocationApi
.getLastLocation(mGoogleApiClient);
if (mLastLocation == null) {
Log.d(TAG, "get last location = null");
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); //then nothing happens
}
else {
Log.d(TAG, "last location successful");
handleNewLocation(mLastLocation);
}; }
@Override
public void onLocationChanged(Location location){
handleNewLocation(location);
}
private void handleNewLocation(Location location) {
Log.d(TAG, location.toString());
double currentLatitude = location.getLatitude();
double currentLongitude = location.getLongitude();
LatLng latLng = new LatLng(currentLatitude, currentLongitude);
MarkerOptions options = new MarkerOptions()
.position(latLng)
.title("I am here!");
mMap.addMarker(options);
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
Address address = getAddress(location);
starttext.setText("Current Location: \n" + address.toString());
}