I am using Google Maps API for my application. My GPS location is changing even when device is at the same place, the location is said to be updated every 6s and thus it keeps updating but it also moves a little with each update and sometimes a lot. My code is below. And I'll also add screenshots.
package b.a.location_tracker;
import android.graphics.Color;
import android.location.Location;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.util.Log;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.Polyline;
import com.google.android.gms.maps.model.PolylineOptions;
public class Map extends FragmentActivity implements OnMapReadyCallback, LocationListener, GoogleApiClient.ConnectionCallbacks {
private GoogleMap mMap;
Marker m;
private GoogleApiClient mGoogleApiClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
this.buildGoogleApiClient();
mGoogleApiClient.connect();
}
private synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this).addApi(LocationServices.API)
.build();
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng sydney = new LatLng(28.6139, 77.2090);
m=mMap.addMarker(new MarkerOptions().position(sydney).title("Delhi City"));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 20));
}
@Override
public void onConnected(Bundle bundle) {
LocationRequest mLocationRequest = createLocationRequest();
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, (LocationListener) this);
}
@Override
public void onConnectionSuspended(int i) {
Log.d("TAG", "Connection to Google API suspended");
}
private LocationRequest createLocationRequest() {
LocationRequest mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(6000);
mLocationRequest.setFastestInterval(1000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
return mLocationRequest;
}
@Override
public void onLocationChanged(Location location) {
LatLng sydney = new LatLng(location.getLatitude(), location.getLongitude());
LatLng a=m.getPosition();
m.remove();
m=mMap.addMarker(new MarkerOptions().position(sydney).title("Your Location"));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, mMap.getCameraPosition().zoom));
Polyline line = mMap.addPolyline(new PolylineOptions().add(a,sydney).width(5).color(Color.RED));
}
}
Do remember my phone is in the same place hence it should not be moving.
This is a screenshot of the app