So as the title says, I'm following the code found on the Android developer site and attempting to get the latitude and longitude of my current location. I'm using Android Studio and an emulator. I have no idea why my code is passing null when I use getLatitude() and getLongitude() - I thought I followed the code as specified.
Thanks!
(What's needed of the class) HomePage.java:
public void onConnected(@Nullable Bundle bundle) {
if (location == null){
declareLocation();
}
double latitude = location.getLatitude();
double longitude = location.getLongitude();
SharedPreferences sharedPreferences = getSharedPreferences("example.com.musicapptest", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putLong("latitude", Double.doubleToLongBits(latitude));
editor.putLong("longitude", Double.doubleToLongBits(longitude));
}
private Location declareLocation(){
try{
location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
}
catch (SecurityException e){
e.printStackTrace();
}
return location;
}