Good day.
I need to get location everytime the application is launching. The location gathering is happening inside the fragment like so.
I am bulding client like this.
mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
// The next two lines tell the new client that “this” current class will handle connection stuff
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
//fourth line adds the LocationServices API endpoint from GooglePlayServices
.addApi(LocationServices.API)
.build();
// Create the LocationRequest object
mLocationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_LOW_POWER);
@Override
public void onStart() {
super.onStart();
if (mSharedHelper.showGreeting()) {
if (!mGoogleApiClient.isConnected()) {
mGoogleApiClient.connect();
}
}
}
@Override
public void onDestroy() {
super.onDestroy();
if (mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
}
The onConnected
callback is being triggered and I am doing the next.
@Override
public void onConnected(Bundle bundle) {
try {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (location == null) {
failedLocate = true;
hideGreetingCard();
} else {
failedLocate = false;
//If everything went fine lets get latitude and longitude
currentLatitude = location.getLatitude();
currentLongitude = location.getLongitude();
YahooWeather yahooWeather = YahooWeather.getInstance();
yahooWeather.queryYahooWeatherByLatLon(getActivity(), currentLatitude, currentLongitude, this);
}
} catch (SecurityException e) {
e.printStackTrace();
}
}
The issue is that the location variable is always null no matter what priority i have tried whatever i did nothing works, i got all of the permissions added inside manifest and requested at the runtime accordingly, and besides location variable being null, the onLocationChanged
callback is never ever getting triggered but yet you can see that inside onConnected
i am requesting the location update but nothing just simply works and no i do not need to use any kind of map to force any kind of simulation to happen i need just simply get the longitude and latitude of the user whenever the app is launched and this fragment is being shown thats much i want.
Any ideas?