简体   繁体   中英

Google maps api v2 polylines android

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

Ok, in order to "redraw" the Polyline you have two options:

  1. Simulate it by removing it using Polyline.remove() . Then build it again using PolylineOptions and add with GoogleMap.addPolyline() .

  2. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM