Multi color Polyline in google map v2 in android

2019-07-12 21:52发布

I searched a lot i didn't find any proper solution for it.Help and link could be appreciated :-)multicolored polyine

2条回答
趁早两清
2楼-- · 2019-07-12 22:26

Try this -

@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)); 

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

查看更多
爷的心禁止访问
3楼-- · 2019-07-12 22:42

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

 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);
                    }
                }
            }
        });

    }
}

enter image description here

enter image description here

Ok it's my screenshot , color will be change by changing speed of car

查看更多
登录 后发表回答