Hi I am developing android location tracker.I have developed two application one is for sending location of driver and other is for tracking the location of driver. Application is running well but sometimes my driver application's location is unable to connect with server and stuck on last location and doesnot reconnect().Sometimes it works perfect.Here is my code of driver app.
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);
}