【问题标题】:Android Google Maps CustomCap appearing behind polylineAndroid Google Maps CustomCap 出现在折线后面
【发布时间】:2019-04-17 03:30:51
【问题描述】:

我正在使用折线在 Android 中的 Google 地图上绘制路线。我希望在我的第一条折线上有一个起始上限,向用户显示路线开始的位置。

但是,当我显示的路线必须用多条折线显示并且最后一条折线在第一条折线开始的位置附近结束时,我有时会看到起始端显示在最后一条折线后面。

这种情况并非每次都会发生。我想确保起始上限始终显示在任何和所有折线上方。如何确保帽子显示在所有折线之上?

这是我用来在地图上绘制路线的方法:

private fun addRoute(context: Context, map: GoogleMap, mapData: MapData, color: Int) {

            mapData.routeSegments.forEachIndexed { index, segment ->

                val polylineOptions = PolylineOptions()

                segment.points.forEach { point ->

                    polylineOptions.add(LatLng(point.latitude, point.longitude))
                }

                val polyline = map.addPolyline(polylineOptions)

                polyline.isClickable = false
                polyline.color = color
                polyline.width = POLYLINE_WIDTH

                if (index == 0) {
                    val bitmap = DrawableUtil.getBitmapFromVectorDrawable(context, R.drawable.star_red_28px)
                    polyline.startCap = CustomCap(BitmapDescriptorFactory.fromBitmap(bitmap))
                }

            }
        }

预期:

实际:

【问题讨论】:

    标签: java android google-maps kotlin


    【解决方案1】:

    Line caps 为折线的每一端指定Cap 样式。似乎它不是显示在所有顶部,而是显示在其折线顶部。因此,为了确保上限显示在所有折线的“顶部”,您应该为每条折线累积set Z-index,并从“全局”开始到结束绘制折线:在这种情况下,下一段的startCap 与当前折线的末端重叠。或者你不能使用 Caps,而是使用 markers 和自定义 R.drawable.star_red_28px 图标:

    ...
    LatLng polylineStartLatLng = polyline.getPoints().get(0);
    
    Drawable starDrawable = ContextCompat.getDrawable(getApplicationContext(), R.drawable.star_red_28px);
    Canvas canvas = new Canvas();
    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    canvas.setBitmap(bitmap);
    starDrawable.setBounds(0, 0, 28, 28);
    starDrawable.draw(canvas);
    BitmapDescriptor markerIcon = BitmapDescriptorFactory.fromBitmap(bitmap);
    mGoogleMap.addMarker(new MarkerOptions()
            .position(polylineStartLatLng)
            .anchor(0.5f, 0.5f)
            .icon(markerIcon)
    );
    ...
    

    【讨论】:

    • 谢谢,@Andrii。在为所有不包含 startCap 的折线设置 z-index 以将它们“放在”包含 startCap 的折线“后面”之后,看来我的问题已解决。
    猜你喜欢
    • 1970-01-01
    • 2014-02-07
    • 2014-02-16
    • 2016-08-25
    • 1970-01-01
    • 2018-12-29
    • 1970-01-01
    • 2013-06-29
    相关资源
    最近更新 更多