Я много искал, я не нашел подходящего решения для этого. Помощь и ссылка могут быть оценены :-)
Многоцветная полилиния на карте Google v2 в Android
Ответы (3)
может так поздно!!! но я решаю эту проблему и хочу поставить ее для некоторых людей. может быть полезно. и для деталей я решаю это с помощью новой полилинииOption каждые 5 Latlng на карте
private static void animateMarker(final GoogleMap map, final Marker marker, final List<LatLng> directionPoint,
final boolean hideMarker, final List<Float> degree, final List<Integer> colors) {
final Handler handler = new Handler();
final long start = SystemClock.uptimeMillis();
final long duration = 300000;
final PolylineOptions[] polylineOptions = {new PolylineOptions()};
final Interpolator interpolator = new LinearInterpolator();
if (map != null) {
handler.post(new Runnable() {
int i = 0;
@Override
public void run() {
long elapsed = SystemClock.uptimeMillis() - start;
float t = interpolator.getInterpolation((float) elapsed / duration);
if (i < directionPoint.size() - 1) {
final LatLng currentPosition = new LatLng(
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] = new PolylineOptions();
polylineOptions[0].add(directionPoint.get(i)).color(colors.get(i));
map.addPolyline(polylineOptions[0]);
}
CameraUpdate cameraUpdate = 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);
}
}
}
});
}
}
Хорошо, это мой скриншот, цвет будет меняться в зависимости от скорости автомобиля.
Попробуй это -
@Override
public void onMapReady(GoogleMap map) {
// Add a thin red line from A to B.
Polyline line1 = map.addPolyline(new PolylineOptions()
.add(new LatLng(40.1, -74.2), new LatLng(40.7, -74.0))
.width(5)
.color(Color.RED));
а затем еще одна линия от B до C с другим цветом и так далее
Polyline line2 = map.addPolyline(new PolylineOptions()
.add(new LatLng(40.7, -74.0), new LatLng(41.3, -74.5))
.width(5)
.color(Color.GREEN));
....
Обратите внимание, что getMapAsync() — это новый предпочтительный способ получения объекта карты. https://developers.google.com/maps/documentation/android-api/map
Детали полилинии здесь — https://developers.google.com/android/reference/com/google/android/gms/maps/model/Polyline
Вы могли видеть, что это приводит к разделенным ломаным линиям. Это можно улучшить, добавив круглые начальные и конечные концы каждой полилинии.
polyline.setEndCap(new RoundCap());
polyline2.setStartCap(new RoundCap());