【问题标题】:How to detect if the current location is inside an area with CoreLocation如何检测当前位置是否在具有 CoreLocation 的区域内
【发布时间】:2012-05-24 07:04:19
【问题描述】:

我希望我的应用程序知道用户是否在特定区域内。我一直在学习地理围栏,但我不希望设备不断检查它是否进入或离开一个区域,我只想知道它是否在那个具体时刻就在上面。另外,我读到地理围栏的精度(似乎)很低,而且我需要的不仅仅是蜂窝塔的精度。
所以想法是使用“标准”类型的位置来执行此操作,但我不知道如何在给定新的当前位置的情况下检查它是否在(圆形、矩形、多边形?)区域内。
它可能必须使用纯数学来完成,检查高度是否在 2 个参数之间,以及经度?有没有更简单的方法?

谢谢!

【问题讨论】:

    标签: objective-c geolocation core-location region


    【解决方案1】:

    【讨论】:

    • 好的,这适用于圆形区域。如果我想定义一个多边形区域怎么办?基本上,检查设备是通过路径循环还是在路径外部(例如,考虑 10 米宽的路径)。感谢回复!
    • iOS 中没有内置的东西来确定一个点是否在多边形内。您可以使用具有纬度/经度坐标的基本“多边形中的点”算法,但由于地理投影的工作方式,它们在较大比例下并不可靠。我从未使用过它,但 ShapeKit github.com/mweisman/ShapeKit 看起来可以满足您的需求。
    • 我想我会用distanceFromLocation 和几个 if's&else's 来做,这样我就可以检测到交叉点。就我的目的而言,它应该可以正常工作。无论如何感谢您的帮助! :)
    【解决方案2】:

    这是我的乔丹曲线定理的 Swift 代码。只需提供一个 CLLocationCoordinate2D 数组,它可以制作正方形、多边形等任何东西。

    更深入的答案我翻译自: How can I determine whether a 2D Point is within a Polygon?

    原网址:PNPOLY - Point Inclusion in Polygon Test W. Randolph Franklin (WRF)

    extension CLLocationCoordinate2D {
    
        func liesInsideRegion(region:[CLLocationCoordinate2D]) -> Bool {
    
            var liesInside = false
            var i = 0
            var j = region.count-1
    
            while i < region.count {
    
                guard let iCoordinate = region[safe:i] else {break}
                guard let jCoordinate = region[safe:j] else {break}
    
                if (iCoordinate.latitude > self.latitude) != (jCoordinate.latitude > self.latitude) {
                    if self.longitude < (iCoordinate.longitude - jCoordinate.longitude) * (self.latitude - iCoordinate.latitude) / (jCoordinate.latitude-iCoordinate.latitude) + iCoordinate.longitude {
                        liesInside = !liesInside
                        }
                }
    
                i += 1
                j = i+1
            }
    
            return liesInside
        }
    
    }
    

    编辑

    安全数组项

    extension MutableCollection {
        subscript (safe index: Index) -> Iterator.Element? {
            get {
                guard startIndex <= index && index < endIndex else { return nil }
                return self[index]
            }
            set(newValue) {
                guard startIndex <= index && index < endIndex else { print("Index out of range."); return }
                guard let newValue = newValue else { print("Cannot remove out of bounds items"); return }
                self[index] = newValue
            }
        }
    }
    

    【讨论】:

    • 感谢算法的 Swift 翻译,您能解释一下算法中“安全”变量的含义吗?我必须将其设置为 0 吗?
    • 啊,我为数组制作了一个包装器,以便它可以在索引处返回可选...让我为你添加它
    • 非常感谢您的解释!
    • 嗨,@Magoo 感谢您的回答。我认为这个解决方案适用于“凸”而不是“非凸”多边形。对吗?
    • 我认为 Jordan 曲线定理适用于任何...它通过检查 x 相交的次数来工作...maths.ed.ac.uk/~v1ranick/jordan/cr.pdf
    【解决方案3】:

    如果您使用 Google Map SDK 并想检查一个点是否在多边形内,您可以尝试使用GMSGeometryContainsLocation。如果多边形不是凸的,@Magoo 的答案效果很好。但是如果它是凹的,那么@Magoo的方法没有帮助。

    if GMSGeometryContainsLocation(point, polygon, true) {
        print("Inside this polygon.")
    } else {
        print("outside this polygon")
    }
    

    这是参考:https://developers.google.com/maps/documentation/ios-sdk/reference/group___geometry_utils#gaba958d3776d49213404af249419d0ffd

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-23
      • 2015-06-03
      • 2018-02-04
      • 1970-01-01
      • 2011-09-24
      相关资源
      最近更新 更多