I have a method with an asynctask inside.
public void getNearest(){
new AsyncTask() {
@Override
protected void onPreExecute(){
super.onPreExecute();
}
@Override
protected Object doInBackground(Object[] objects){
buildGoogleApiClient();
if (ContextCompat.checkSelfPermission(MapsActivity.this,
Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
mGoogleApiClient);
}
return null;
}
@Override
protected void onPostExecute(Object o) {
Collections.sort(marker, new Comparator<Location>() {
@Override
public int compare(Location a, Location b) {
Location locationA = new Location("point A");
Location locationB = new Location("point B");
float distanceOne = mLastLocation.distanceTo(locationA);
float distanceTwo = mLastLocation.distanceTo(locationB);
return Float.compare(distanceOne, distanceTwo);
}
});
}
}.execute();
}
I want the doInBackground
method to get the current location. This whole method is to be called in the onCreate
method so I can have a variable for the current location. However, the app crashes and says that GoogleApiClient should be a parameter. It points to the doInBackground
method. I am new to coding with the google maps api so I'm really having a hard time. Any help will be appreciated thank you very much!
buildGoogleApiClient():
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mGoogleApiClient.connect();
}