在多边形上添加孔会删除其填充颜色

时间:2019-07-31 23:05:20

标签: android google-maps kotlin

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

添加几个孔后,地图可以正常工作。使用多边形填充颜色为地图着色,并渲染孔。

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

我的漏洞列表大小为63,但可以有所不同。

多边形可以有多少个孔?或如何绘制带有多个孔的多边形?所有的孔都包含在地图中。

编辑1

我无法添加确切的代码段,但这与我正在执行的操作类似。

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

1 个答案:

答案 0 :(得分:1)

问题似乎出在您孔的位置。 Google's documentation在“多边形”中表示:

  

如果孔与多边形的轮廓相交,则多边形将为   呈现没有任何填充。

然后Polygon's reference声明:

  

     

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

     

填充颜色

     

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

希望这会有所帮助!