首页 > 解决方案 > 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 个地方获取位置并获得相同的值。请帮我找出我的错误。对不起,我的英语不好。

谢谢你的支持。

标签: androidgoogle-location-servicesfusedlocationproviderclient

解决方案


推荐阅读