Multi color Polyline in google map v2 in android

前端 未结 3 1719
长发绾君心
长发绾君心 2021-01-21 03:35

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

相关标签:
3条回答
  • 2021-01-21 03:43

    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(new RoundCap());
    polyline2.setStartCap(new RoundCap());
    
    0 讨论(0)
  • 2021-01-21 04:02

    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

    0 讨论(0)
  • 2021-01-21 04:07

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

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

    0 讨论(0)
提交回复
热议问题