i want to draw path on google map.
Basically what i am tracking user locaion after specfic time interval. when user reach to some destination then i need to draw the
Adding to @antonio answer. You can specify priority and displacement as follows. This will make sure you can receive maximum possible accuracy
private int mInterval =5;
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10;
private static final float MIN_ACCURACY = 10;
private Location lastKnownLocation;
@Override
public void onConnected(Bundle bundle) {
locationRequest = LocationRequest.create();
locationRequest.setInterval(mInterval * 1000); // milliseconds
locationRequest.setFastestInterval(mInterval * 1000);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setSmallestDisplacement(MIN_DISTANCE_CHANGE_FOR_UPDATES);//distance change
int permissionCheck = ContextCompat.checkSelfPermission(appContext, Manifest.permission.ACCESS_COARSE_LOCATION);
if (permissionCheck!= PackageManager.PERMISSION_DENIED)
{ LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient,
locationRequest, this);
}
}
Now in the onLocationChanged method do as per suggestion.
@Override
public void onLocationChanged(Location location) {
if (location.getAccuracy() < MIN_ACCURACY ) {
if (lastKnownLocation == null || lastKnownLocation.distanceTo(location) > MIN_DISTANCE_CHANGE_FOR_UPDATES) {
lastKnownLocation = location;
}
}
}
NOTE :
While retrieving location from NetworkProvider which is faster, even if device is static the received location within specific intervals varies. The above logic will give us the opportunity to avoid the irrelevant values and track/process the valid ones.
Fused Location Provider Api Details
The problem is not on your drawing function but on the method you are using to get the points of your path using the received location.
You need to take into account two factors:
Location.getAccuracy()
method (documentation). The accuracy is measured in meters, so the lower the better.An example combining both considerations:
private static final float MINIMUM_ACCURACY = 10; // If the accuracy of the measurement is worse than 10 meters, we will not take the location into account
private static final float MINIMUM_DISTANCE_BETWEEN_POINTS = 20; // If the user hasn't moved at least 20 meters, we will not take the location into account
private Location lastLocationloc;
// ...
public void onLocationChanged(Location mylocation) {
if (mylocation.getAccuracy() < MINIMUM_ACCURACY) {
if (lastLocationloc == null || lastLocationloc.distanceTo(mylocation) > MINIMUM_DISTANCE_BETWEEN_POINTS) {
// Add the new location to your polyline
lastLocationloc = mylocation;
}
}
}
I'm agree with antonio's answer. Problem is not with you drawing function. You just drawing path whatever device giving you location. Problem is your device is not giving accurate location.
Let me giving you brief description about Location Functionality.
There are three kind of provider giving location in device.
This provider determines location using satellites. Depending on conditions, this provider may take a while to return a location fix. Requires the permission ACCESS_FINE_LOCATION.
The extras Bundle for the GPS location provider can contain the following key/value pairs:
satellites - the number of satellites used to derive the fix
This provider determines location based on availability of cell tower and WiFi access points. Results are retrieved by means of a network lookup.
This provider can be used to passively receive location updates when other applications or services request them without actually requesting the locations yourself. This provider will return locations generated by other providers. You can query the getProvider() method to determine the origin of the location update. Requires the permission ACCESS_FINE_LOCATION, although if the GPS is not enabled this provider might only return coarse fixes.
Reference:
Android Location Manager
Android Location Providers - GPS or Network Provider?
As per my experience I can say
GPS Provider
is giving you accurate location compare toNetwork Provider
but need open environment,GPS provider
is not giving accurate location in inside building.
Returns a constant describing horizontal accuracy of this provider. If the provider returns finer grain or exact location, ACCURACY_FINE
is returned, otherwise if the location is only approximate then ACCURACY_COARSE
is returned.
This method will return exact value when location is changed from GPS Provider otherwise it will return approximate or 0 value.
Ref : Location.getAccuracy()
Solution:
Use only GPS Provider
Find out your location is accurate or not.
Recommended Use Google Client Sample Example.
Have you looked into the new Roads Api? It will calculate the accuracy and returns the most reasonable values to draw the line from. It also provides the speed limit on the current road.