我有一个带有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();
}