0

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();
}
Paradigm
  • 159
  • 1
  • 3
  • 11
  • Could you show your `buildGoogleApiClient` implementation? – antonio Feb 06 '17 at 15:50
  • Post updated sir! – Paradigm Feb 06 '17 at 15:59
  • Your `mGoogleApiClient` may not be connected when you call `LocationServices.FusedLocationApi.getLastLocation`. You should call `getLastLocation` on the `onConnected` method. [Here](http://stackoverflow.com/questions/37986082/android-googlemaps-mylocation-permission/37986247#37986247) is a working example – antonio Feb 06 '17 at 16:05

0 Answers0