【问题标题】:How to determine if an GMSMarker is inside of GMSPolygon (iOS Google Map SDK)如何确定 GMSMarker 是否在 GMSPolygon 内(iOS Google Map SDK)
【发布时间】:2014-07-18 13:49:34
【问题描述】:

我知道如何在this link 之后使用 Apple MapKit 执行此操作,但不知道如何检查位置或注释 (GMSMarker) 是否在 Google Map SDK 的 GMSPolygon 内。

【问题讨论】:

  • 我在 JavaScript 中的 Google 地图中找到了如何做到这一点...来自此链接,stackoverflow.com/questions/20074747/… 但没有找到适用于 iOS 的任何解决方案
  • stackoverflow.com/questions/19587792/…。使用 GMSGeometryContainsLocation。即使没有内置的 Google 方法,您也可以使用here 所示的 CGPathContainsPoint 方法,使用多边形的坐标构建您自己的 CGPathRef。

标签: ios objective-c google-maps ios7


【解决方案1】:

我得到了答案。

Google SDK 的 GMSGeometryUtils.h 文件中定义了一个内联函数:

//Returns whether |point|,CLLocationCoordinate2D lies inside of path, GMSPath
    BOOL GMSGeometryContainsLocation(CLLocationCoordinate2D point, GMSPath *path,
                                 BOOL geodesic);

它包含一个名为 GMSGeometryContainsLocation 的函数,用于确定位置点是否位于多边形路径内。

if (GMSGeometryContainsLocation(locationPoint, polygonPath, YES)) {
    NSLog(@"locationPoint is in polygonPath.");
} else {
    NSLog(@"locationPoint is NOT in polygonPath.");
}

Source: GMSGeometryContainsLocation

【讨论】:

  • 这个对我不起作用,它在这里崩溃 if (GMSGeometryContainsLocation(locationPoint, polygonPath, YES)) {
  • 检查 locationPoint 和 polygonPath 的 NULL 值。
【解决方案2】:

对于 Swift 4,您可以使用这个扩展:

extension GMSPolygon {

    func contains(coordinate: CLLocationCoordinate2D) -> Bool {

        if self.path != nil {
            if GMSGeometryContainsLocation(coordinate, self.path!, true) {
                return true
            }
            else {
                return false
            }
        }
        else {
            return false
        }
    }
}

你可以像这样直接从你的多边形对象中调用它:

if gms_polygon.contains(coordinate: point) {
    //do stuff here
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-17
    • 2016-07-05
    • 1970-01-01
    • 1970-01-01
    • 2016-08-23
    • 2013-02-17
    • 1970-01-01
    • 2015-08-31
    相关资源
    最近更新 更多