【问题标题】:Adding holes to a polygon removes it's fill color向多边形添加孔会删除它的填充颜色
【发布时间】:2019-07-31 23:05:20
【问题描述】:

我创建了一个填充整个 Google 地图的多边形。

添加几个洞后,地图工作正常。地图被多边形填充颜色着色,并且孔被渲染。

但是,添加第三个孔后,孔仍然显示多边形的笔触,但多边形填充颜色变为透明。

我的洞列表大小是 63,但它可能会有所不同。

多边形可以有的孔有任何限制吗?或者如何绘制一个带有多个孔的多边形?所有的洞都包含在地图中。

编辑 1

我无法添加我工作中的确切代码 sn-p,但这与我正在做的事情相似。

fun GoogleMap.render(geoJSONS: List<JSONObject>) {
    val wholeMapPolygon = PolygonOptions().add(
        LatLng(-89.999999999999, -180.0),
        LatLng(89.99999999999, -180.0),
        LatLng(89.99999999999, 179.99999999),
        LatLng(-89.99999999999, 179.99999999),
        LatLng(-89.99999999999, 0.0)
    ).strokeColor(Color.BLACK)
     .fillColor(Color.WHITE)

    val layers = createGeoJSONLayers(this, geoJSONS)
    val holes = getPolygonsFromGeoJsonLayers()

    for(hole in holes) {
        wholeMapPolygon.addHole(hole) // Here, adding more that 3 makes the wholeMapPolygon fill color dissapear.
    }

    addPolygon(wholeMapPolygon)
}

/**
* In this function, some other functions get called to extract each list of coordinates for each layer polygon.
**/
private fun getPolygonsFromGeoJsonLayers(layers: List<GeoJsonLayer>): List<List<LatLng>> {
   val holes = mutableListOf<List<LatLng>>()
   for(layer in layers) {
      val polygonFeatures = getGeoJsonLayerPolygonFeatures()
      for(polygonFeature in polygonFeatures) {
         holes.addAll(getCoordinatesFromFeature(polygonFeature))
      }
   }
   return holes
}

private fun getGeoJsonLayerPolygonFeatures(features: List<GeoJsonFeature>): List<GeoJsonFeature> = features.filter { feature ->
   feature.geometry.geometryType == "Polygon"
}

private fun getCoordinatesFromFeature(feature: GeoJSONFeature): List<LatLng> {
    val coordinates = mutableListOf<LatLng>()
    (geometry as? GeoJsonPolygon)?.coordinates?.filter {
        it.isNotEmpty()
    }?.forEach {
        coordinates.addAll(it)
    }
    return coordinates
}

private fun createGeoJSONLayers(map: GoogleMap, data: List<JSONObject>) = data?.map { jsonObject ->
   GeoJsonLayer(map, jsonObject)
}

【问题讨论】:

  • 贴一些代码和一些截图
  • 我无法发布确切的代码,因为它来自我公司,我会发布类似的内容

标签: android google-maps kotlin


【解决方案1】:

问题似乎在于您的孔的位置。多边形上的Google's documentation 说:

如果孔与多边形的轮廓相交,则多边形将 无任何填充渲染。

Polygon's reference 声明:

孔必须完全包含在轮廓内。多个孔可以 可以指定,但不支持重叠孔。

填充颜色

默认值为透明 (0x00000000)。如果多边形几何 未正确指定(请参见上文的轮廓和孔),则没有 将绘制填充。

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 2013-02-21
    • 1970-01-01
    • 1970-01-01
    • 2013-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多