有时我的应用程序被困在旧位置并且不会更新

时间:2017-10-31 17:43:31

标签: java android google-maps-android-api-2 locationmanager google-location-services

您好我正在开发Android位置跟踪器。我开发了两个应用程序,一个是发送驱动程序的位置,另一个是跟踪驱动程序的位置。应用程序运行良好,但有时我的驱动程序应用程序的位置无法与服务器连接并停留在最后一个位置并且不重新连接()。有时它工作正常。这是我的驱动程序应用程序代码。

This is the screenshot of my tracking app which shows that driver app is stuck of last updated time after that it is not reconnecting.

 private void setMapToCurrentLocation() {
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
            != PackageManager.PERMISSION_GRANTED) {
        Toast.makeText(this, "First allow location permission", Toast.LENGTH_LONG).show();
        return;
    }
    Location location = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);

    if (location != null) {
        BusLocation busLocation = new BusLocation(location.getLatitude(), location.getLongitude(), 0, true, getCurrentTime());
        mDatabase.child(String.valueOf(bus.getId())).setValue(busLocation);
        moveMap(location);
    }
}

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

@Override
protected void onStop() {
    googleApiClient.disconnect();
    super.onStop();
}


@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    drawMarkers();
}

@Override
public void onConnected(@Nullable Bundle bundle) {
    setMapToCurrentLocation();
}

@Override
public void onConnectionSuspended(int i) {

}

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

}

@Override
public void onLocationChanged(final Location location) {
    moveMap(location);
    lastLocation = location;
    float speed = Constants.convertSpeedToKMPH(location.getSpeed());
    BusLocation busLocation = new BusLocation(location.getLatitude(), location.getLongitude(), speed, true, getCurrentTime());
    mDatabase.child(String.valueOf(bus.getId())).setValue(busLocation);
    textViewSpeed.setText(speed + " KMPH");
    checkDistanceFromStop();
}

@Override
public void onStatusChanged(String s, int i, Bundle bundle) {

}

@Override
public void onProviderEnabled(String s) {

}

@Override
public void onProviderDisabled(String s) {

}

@Override
public void onClick(View view) {
    locationManager.removeUpdates(this);
}

0 个答案:

没有答案