Reputation: 41
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
Upvotes: 3
Views: 3105
Reputation: 101
Remove these two lines from your code:
mLocationRequest.setInterval(6000);
mLocationRequest.setFastestInterval(1000);
Upvotes: 0
Reputation: 6663
Did you tried mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
When i am using LocationRequest.PRIORITY_HIGH_ACCURACY
the Gps is always using and always changing in buildings . And i changed accuracy then the location changing less than.
You can take a look at this :
/*
Priority update interval Battery drain per hour (%) Accuracy
HIGH_ACCURACY 5 seconds 7.25% ~10 meters
BALANCED_POWER 20 seconds 0.6% ~40 meters
NO_POWER N/A small ~1 mile
*/
And using :
public static void createLocationRequest() {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(UPDATE_INTERVAL);
mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
mLocationRequest.setSmallestDisplacement(DISPLACEMENT);
}
private static int UPDATE_INTERVAL = 20000; // 20 second
private static int FASTEST_INTERVAL = 10000; // 10 second
private static int DISPLACEMENT = 50; // minimum change to get location
Upvotes: 0
Reputation: 560
Did you try to add minDisplacementDistance to your LocationRequest ?
private LocationRequest createLocationRequest() {
LocationRequest mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(6000);
mLocationRequest.setFastestInterval(1000);
mLocationRequest.setSmallestDisplacement(200);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
return mLocationRequest;
}
If you will set this parameter (by default it's equal 0) , on LocationChanged listener will not be called each time if your new position which GPS find is inside 200 meters radius of previus found position.
Right now your onLocationChange listener is called each 6 seconds and the minimum distance is equal 0. Since GPS is not 100% correct, you will get a lot of different positions. After you change smallestDisplacement to higher, that's will be not called so often.
Upvotes: 1