I want to get the location of the device at app startup.
I have been looking through the docs, trying to figure out what goes wrong, but I can't seem to find it. onLocationChanged is never called.
private void getLocation() {
// Connected to Google API
// Create location request with minimum interval of a minute, and normal of an hour
mLocationRequest = new LocationRequest();
mLocationRequest.setPriority(PRIORITY_BALANCED_POWER_ACCURACY);
mLocationRequest.setFastestInterval(1000 * 60);
mLocationRequest.setInterval(1000 * 60 * 60);
// Request permissions
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{"ACCESS_FINE_LOCATION"}, MY_PERMISSION_LOCATION_CODE);
return;
}
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient, mLocationRequest, this);
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case MY_PERMISSION_LOCATION_CODE: {
if ((grantResults.length > 0) && (grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient, mLocationRequest, this);
}
else {
TextView loading = (TextView)findViewById(R.id.connecting_text);
loading.setText("Please enable Location under Settings > Apps > Beet > Permissions.");
loading.setTextSize(20);
}
break;
}
}
}
public void onLocationChanged(Location location) {
System.out.println("GETLOCATION1 ________________");
mLastLocation = location;
System.out.println(location.getLatitude() + " - " + location.getLongitude() + " ________________");
}