My question titles seems to be an existing one, but here is my complete scenario.
I have an activity for Map based operations, where am drawing a polyline along a road, lets say a route between two locations. Basically the app tracks the users current location (Traveling by car). So till part everything is working, as in, the route is properly shown, device Location API is giving location updates (kindof exact), and also i was able to change the location updates smoothly,
So the issue is, the locations updates are sometimes zig zag, it might not touch the road sometimes, the location updates will be going all over the place.
I have looked into ROAD api also, but am not getting the correct help, even from some previously asked questions.
Will it be possible to make the marker move only along the road?
Any kind of help will be appreciated.
You can snap marker to the path by projection of marker on nearest path segment. Nearest segment you can find via
PolyUtil.isLocationOnPath():PolyUtil.isLocationOnPath(carPos, segment, true, 30)and projections of marker to that segment you can find via converting geodesic spherical coordinates into orthogonal screen coordinates calculating projection orthogonal coordinates and converting it back to spherical (
WGS84 LatLng -> Screen x,y -> WGS84 LatLng):With full source code:
you'll got something like that:
But better way is to calculate car distance from start of the path and find it position on path via
SphericalUtil.interpolate()because if several path segments is close one to another (e.g. on different lanes of same road) like that:to current car position may be closest "wrong" segment. So, calculate distance of the car from the start of the route and use
SphericalUtil.interpolate()for determine point exactly on path.