Multi Color Polyline In Google Map V2 In Android
I searched a lot i didn't find any proper solution for it.Help and link could be appreciated :-)
Solution 1:
maybe its so late !!! but i solve this problem and want put it for some people. maybe useful. and for detail i solve it by new polylineOption every 5 Latlng in map
privatestaticvoidanimateMarker(final GoogleMap map, final Marker marker, final List<LatLng> directionPoint,
finalboolean hideMarker, final List<Float> degree, final List<Integer> colors) {
finalHandlerhandler=newHandler();
finallongstart= SystemClock.uptimeMillis();
finallongduration=300000;
final PolylineOptions[] polylineOptions = {newPolylineOptions()};
finalInterpolatorinterpolator=newLinearInterpolator();
if (map != null) {
handler.post(newRunnable() {
inti=0;
@Overridepublicvoidrun() {
longelapsed= SystemClock.uptimeMillis() - start;
floatt= interpolator.getInterpolation((float) elapsed / duration);
if (i < directionPoint.size() - 1) {
finalLatLngcurrentPosition=newLatLng(
directionPoint.get(i).latitude * (1 - t) + directionPoint.get(i + 1).latitude * t,
directionPoint.get(i).longitude * (1 - t) + directionPoint.get(i + 1).longitude * t);
marker.setRotation(degree.get(i));
marker.setPosition(currentPosition);
polylineOptions[0].add(directionPoint.get(i)).color(colors.get(i));
map.addPolyline(polylineOptions[0]);
if (i % 5 != 0) {
polylineOptions[0] = newPolylineOptions();
polylineOptions[0].add(directionPoint.get(i)).color(colors.get(i));
map.addPolyline(polylineOptions[0]);
}
CameraUpdatecameraUpdate= CameraUpdateFactory.newLatLng(currentPosition);
map.animateCamera(cameraUpdate);
i++;
}
if (t < 1.0) {
// Post again 100ms later.
handler.postDelayed(this, 100);
} else {
if (hideMarker) {
marker.setVisible(false);
} else {
marker.setVisible(true);
}
}
}
});
}
}
Ok it's my screenshot , color will be change by changing speed of car
Solution 2:
Try this -
@OverridepublicvoidonMapReady(GoogleMap map) {
// Add a thin red line from A to B.Polylineline1= map.addPolyline(newPolylineOptions()
.add(newLatLng(40.1, -74.2), newLatLng(40.7, -74.0))
.width(5)
.color(Color.RED));
and then another line from B to C with a different color and so on
Polyline line2 = map.addPolyline(new PolylineOptions()
.add(new LatLng(40.7, -74.0), new LatLng(41.3, -74.5))
.width(5)
.color(Color.GREEN));
....
Note that getMapAsync() is the new preferred way to get the map object. https://developers.google.com/maps/documentation/android-api/map
Polyline details here - https://developers.google.com/android/reference/com/google/android/gms/maps/model/Polyline
Solution 3:
You could see this results in separated polylines. This could be improved by adding round start caps and round end caps of each polylines.
polyline.setEndCap(newRoundCap());
polyline2.setStartCap(newRoundCap());
Post a Comment for "Multi Color Polyline In Google Map V2 In Android"