FusedLocationProviderClient无法更新位置

时间:2019-10-15 03:32:19

标签: android google-location-services fusedlocationproviderclient

我正在使用Fused Location Provider Google API更新我的应用的位置,但是尽管我在获得价值时移动了设备,但它具有相同的位置。请帮我找出我的错吗?

我正在使用ExpandableListAdapter中的按钮。当用户单击那里的按钮时。我的应用获取位置并存储在数据库中。我试图将LocationCallback放置在活动中的任何位置,但是它没有改变。

我的活动:

public class IndexActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {

String UserId;
ExpandableListView mExpandableListView;
List<Customer> info;
List<MetterChangeLog> log;

private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
private LocationSettingsRequest.Builder builder;
private PendingResult<LocationSettingsResult> result;
private final int REQUEST_LOCATION = 200;
private final int REQUEST_CHECK_SETTINGS = 300;
private Location mLastLocation;

FusedLocationProviderClient mFusedLocationClient;
private LocationCallback mLocationCallback;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_index);

    try {

        GetMetterChangeLogTask mytt = new GetMetterChangeLogTask(IndexActivity.this);
        mytt.execute();

        log = mytt.get();

    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ExecutionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);

    mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(120000); // two minute interval
    mLocationRequest.setFastestInterval(120000);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);

    /*if (mGoogleApiClient == null) {
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
    }*/
    mLocationCallback = new LocationCallback() {
        @Override
        public void onLocationResult(LocationResult locationResult) {
            for (Location location : locationResult.getLocations()) {

                mLastLocation = location;
            }
        };
    };

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addOnConnectionFailedListener(this)
            .addConnectionCallbacks(this)
            .addApi(LocationServices.API)
            .build();
}

@Override
protected void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
}

@Override
protected void onResume() {
    super.onResume();
    mLocationCallback = new LocationCallback() {
        @Override
        public void onLocationResult(LocationResult locationResult) {
            for (Location location : locationResult.getLocations()) {

                mLastLocation = location;
            }
        };
    };

    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
        mFusedLocationClient.requestLocationUpdates(mLocationRequest,
                mLocationCallback,
                Looper.getMainLooper());
    }
}

@Override
protected void onStop() {
    super.onStop();

    if (mGoogleApiClient.isConnected()) {
        mGoogleApiClient.disconnect();
    }
}

@Override
public void onConnected(@Nullable Bundle bundle) {

    mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(2000);
    mLocationRequest.setFastestInterval(2000);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);

    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
        mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, null);
    }


}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

}

@Override
public void onLocationChanged(Location location) {
    mLocationCallback = new LocationCallback() {
        @Override
        public void onLocationResult(LocationResult locationResult) {
            for (Location location : locationResult.getLocations()) {

                mLastLocation = location;
            }
        };
    };
}

然后我将mLastLocation设置为初始ExpandableListView,如下所示:

mExpandableListView = (ExpandableListView) findViewById(R.id.list_customer);
                    ExpandableListAdapter adapter = new ExpandableListAdapter(IndexActivity.this,
                            mExpandableListView, info, SoDoc.substring(0, 1), mLastLocation);

                    mExpandableListView.setItemsCanFocus(true);

                    mExpandableListView.setAdapter(adapter);

在ExpandableListAdapter类中,我使用提交按钮创建了一个持有人以获取位置。

groupHolder.submit.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View v) {
            //button functionalty   ...

            ...
            final Customer index = new Customer();

            index.setLong(mLastlocation.getLongitude());
            index.setLat(mLastlocation.getLatitude());
            ...
}

我试图在3个地方获得位置并获得相同的价值。请帮助我找到我的错。对不起,我的英语不好。

感谢支持。

0 个答案:

没有答案