Android Polyline - Adding point by point

rockyl picture rockyl · Jun 17, 2013 · Viewed 7.1k times · Source

I'm currently having a map, and each 10 meters I use LocationListener to refresh my location and get the new Latitude and Longitude. Now I wish that the route the user is taking will be displayed with a red line. So everytime the OnLocationChange() from LocationListener class is called, I want to update the map with a line between the last location and the new location.

So far I've added the following:

private void initializeDraw() {
    lineOptions = new PolylineOptions().width(5).color(Color.RED);
    lineRoute = workoutMap.addPolyline(lineOptions);
}

during the OnLocationChanged I call this:

drawTrail();

now what should I insert into this function so that each time it adds the newly attained location as a point and draws a line from the last to the new point.

Thanks

Answer

MaciejGórski picture MaciejGórski · Jun 18, 2013

First translate Location into LatLng:

LatLng newPoint = new LatLng(location.getLatitude(), location.getLongitude());

Then add a point to existing list of points:

List<LatLng> points = lineRoute.getPoints();
points.add(newPoint);
lineRoute.setPoints(points);