【问题标题】:Check if a point is inside a Polygon with the Google Maps API使用 Google Maps API 检查点是否在多边形内
【发布时间】:2013-12-03 04:40:18
【问题描述】:

我正在使用 Google Maps API v3 开发一个应用程序,但我不知道如何确定 X 坐标是否在多边形内。

【问题讨论】:

标签: google-maps-api-3 polygon


【解决方案1】:

在 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)段。

【讨论】:

    【解决方案2】:

    您可以使用 Google Maps JS API 的 Geometry Library。有一个名为containsLocation 的函数会告诉您给定的LatLng 是否在Polygon 内。请注意,它是多边形,而不是折线。折线是(正如它的名字所说)一条线。因此,不存在折线内的点。您可以使用containsLocation 函数检查一个点是否在多边形内。

    google.maps.geometry.poly.containsLocation(somePoint, somePolygon)
    

    【讨论】:

    • 但它适用于 JavaScript,如何使用 Objective C Google Maps SDK 为 iOS 做同样的事情??
    猜你喜欢
    • 1970-01-01
    • 2017-05-03
    • 2011-09-25
    • 2012-11-23
    • 2014-04-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多