需要GoogleApiClient参数(获取asynctask中的当前位置)

时间:2017-02-06 15:26:29

标签: android google-maps

我有一个带有asynctask的方法。

 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();
}

我希望doInBackground方法获取当前位置。整个方法将在onCreate方法中调用,因此我可以为当前位置提供变量。但是,该应用程序崩溃并说GoogleApiClient应该是一个参数。它指向doInBackground方法。我是使用谷歌地图api进行编码的新手,所以我真的很难过。非常感谢任何帮助!

buildGoogleApiClient():

protected synchronized void buildGoogleApiClient() {
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
        mGoogleApiClient.connect();
}

0 个答案:

没有答案