【发布时间】:2013-12-03 04:40:18
【问题描述】:
我正在使用 Google Maps API v3 开发一个应用程序,但我不知道如何确定 X 坐标是否在多边形内。
【问题讨论】:
我正在使用 Google Maps API v3 开发一个应用程序,但我不知道如何确定 X 坐标是否在多边形内。
【问题讨论】:
在 iOS 中可以使用 GMSGeometryContainsLocation 来完成
只需创建一个GMSMutablePath,然后填充多边形的顶点并测试该点。
示例(Swift 4.0):
func isWithin(_ point: CLLocationCoordinate2D) -> Bool {
let p = GMSMutablePath()
p.add(CLLocationCoordinate2D(latitude:30.02356126, longitude: -90.07047824))
p.add(CLLocationCoordinate2D(latitude:30.02501037, longitude: -90.0614231))
p.add(CLLocationCoordinate2D(latitude:30.03321034, longitude: -90.0617981))
p.add(CLLocationCoordinate2D(latitude:30.03192855, longitude: -90.07342815))
return GMSGeometryContainsLocation(point, p, true)
}
注意:如果GMSGeometryContainsLocation的最后一个参数设置为true,则GMSMutablePath由大圆段组成,否则为恒向(loxodromic)段。
【讨论】:
您可以使用 Google Maps JS API 的 Geometry Library。有一个名为containsLocation 的函数会告诉您给定的LatLng 是否在Polygon 内。请注意,它是多边形,而不是折线。折线是(正如它的名字所说)一条线。因此,不存在折线内的点。您可以使用containsLocation 函数检查一个点是否在多边形内。
google.maps.geometry.poly.containsLocation(somePoint, somePolygon)
【讨论】: