Reputation: 2227
I need to draw the polylines on the google map from a ArrayList
each time a new LatLng
is found whose color is according to percentage of the current speed to that of max speed of the vehicle.
I am using the following code for that:
for (int i = 0; i < Route.speeds.size(); i++)
{
colorOfGraph = graphColor(Route.speeds.get(i), maxSpeed);
polylineOptions = new PolylineOptions().addAll(Route.points).width(5).color(colorOfGraph);
Polyline polyline = googleMap.addPolyline(polylineOptions);
Route.paths.add(polyline);
}
where Route.speeds
is an ArrayList
having all the speeds maintained in it, maxspeed
is the max speed of the vehicle which changes when maxspeed
of the vehicle changes and color of polyline is according to %age of max speed, as the max speed increases, the color of the previous polylines should also change, so I need to draw it again. So my qs here is how to redraw the polylines each time i get a new latlng.
SO please anyone help me with this problem.
Thanks & Regards
Upvotes: 0
Views: 5628
Reputation: 16064
Ok, in order to "redraw" the Polyline
you have two options:
Simulate it by removing it using Polyline.remove()
. Then build it again using PolylineOptions
and add with GoogleMap.addPolyline()
.
Change the attributes of the polyline. You can set it's color using setColor(int)
and set points using setPoints(List<LatLng>)
. The polyline should automatically redraw itself on the map after you call any of these methods.
In both cases you have to keep references to the created polylines, but you already do it in the line:
Route.paths.add(polyline);
If I were you I would go with the second option since you won't have to recreate the whole Route.paths
collection. Instead, you would have to add just one polyline - the one that goes from the previously-last LatLng
to the last LatLng
recorded.
Upvotes: 3