Android中的谷歌地图v2中的多色折线

时间:2016-11-30 04:43:12

标签: android google-maps google-polyline

我搜索了很多我没有找到任何适当的解决方案。帮助和链接可以被赞赏: - )multicolored polyine

3 个答案:

答案 0 :(得分:1)

试试这个 -

@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

答案 1 :(得分:1)

也许太晚了!但我解决了这个问题,并希望将其提供给某些人。也许有用。详细来说,我每5个地图通过新的polylineOption解决它

 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

好吧,这是我的屏幕截图,颜色会随着汽车速度的变化而改变

答案 2 :(得分:0)

您可能会在分开的折线中看到此结果。 可以通过在每条折线中添加圆角起点和圆角终点来改善这一点。

ForEach(collection.indices, id: \.self) { index in
        Component(myValue: self.collection[index].value)

        .onTapGesture {
                self.choosenstore.choosencard.append(
                    Choosendeck(value: self.collection[index].value)
                )
        }
}