了解GeoJsonLayer是否包含给定点

时间:2018-11-30 16:33:25

标签: android kotlin google-maps-android-api-2 geojson

我试图了解给定点(纬度,经度)是否停留在GeoJsonLayer内。我已经编写了此函数,但是该图层没有边框,因此它不起作用:

private fun isInsideLayer(userLatitude: Double, userLongitude: Double, layer: GeoJsonLayer): Boolean {
        val position = LatLng(userLatitude, userLongitude)
        return layer.boundingBox.contains(position)
    }

下面是我的JSON来源。

 {
      "type": "Feature",
      "properties": {},
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              13.36761474609375,
              52.626602989514865
            ],
            [
              13.379974365234373,
              52.616181784632865
            ],
            [
              13.380317687988281,
              52.606175093642754
            ],
            [
              13.36761474609375,
              52.626602989514865
            ]
          ]
        ]
      }
    }

有人知道吗?谢谢

2 个答案:

答案 0 :(得分:3)

这是一个非常理论上的问题,除非知道存在哪种类型的几何图形-因为几何图形不一定是可以包含点的多边形。否则只能检查一个点是否完全匹配。根本的问题是.getBoundingBox()返回一个矩形-而不是一个多边形,由于周围的多余区域,该矩形不允许正确匹配。

例如为了构造多边形,至少需要3个几何类型的点。

或(至少)1个多边形类型的几何...可以与Utility LibraryPolyUtil匹配:

PolyUtil.containsLocation(position)

一个特征对象总共有10种不同的几何对象,这需要考虑... RFC 7946的提示。

答案 1 :(得分:1)

@Martin Zeitler,谢谢您使我走上正确的道路。 我的JSON源实际上包含一个多边形(问题中添加了源)。因此,通过以下操作从要素中获取多边形非常容易:

private fun getPolygon(layer: GeoJsonLayer): GeoJsonPolygon? {
        for (feature in layer.features) {
            return feature.geometry as GeoJsonPolygon
        }
        return null
    }

此后,使用PolyUtil可以很容易地通过以下操作确定任意点是在多边形内部还是外部:

 private fun isInside(pointLatitude: Double, pointLongitude: Double, layer: GeoJsonLayer): Boolean {
        val polygon: GeoJsonPolygon? = getPolygon(layer)
        return PolyUtil.containsLocation(pointLatitude, pointLongitude, polygon!!.outerBoundaryCoordinates, false)
    }