【问题标题】:Rectangle using CLLocationCoordinate2D使用 CLLocationCoordinate2D 的矩形
【发布时间】:2020-06-22 16:20:40
【问题描述】:

我想知道我是否可以使用左上角和右下角CLLocationCoordinate2D 创建一个矩形,然后通过检查(topLeftCoordinate.latitude < coordinate.latitude && bottomRightCoordinate.latitude > coordinate.latitude) && (topLeftCoordinate.longitude < coordinate.longitude && bottomRightCoordinate.longitude > coordinate.longitude) 来检查一个坐标是否是这个矩形的一部分。

我认为坐标将代表球体上的一个点,然后这行不通。但是由于CLLocationCoordinate2D 末尾的2D,我感到困惑。有人可以澄清一下吗?

谢谢:)

【问题讨论】:

标签: ios swift coordinates core-location


【解决方案1】:

您将能够从您的坐标创建一个 MKPolygon 并使用此函数来确定坐标是否在该多边形内:

func isPoint(point: MKMapPoint, insidePolygon poly: MKPolygon) -> Bool {

    let polygonVerticies = poly.points()
    var isInsidePolygon = false

    for i in 0..<poly.pointCount {
        let vertex = polygonVerticies[i]
        let nextVertex = polygonVerticies[(i + 1) % poly.pointCount]

        // The vertices of the edge we are checking.
        let xp0 = vertex.x
        let yp0 = vertex.y
        let xp1 = nextVertex.x
        let yp1 = nextVertex.y

        if ((yp0 <= point.y) && (yp1 > point.y) || (yp1 <= point.y) && (yp0 > point.y))
        {
            // If so, get the point where it crosses that line. This is a simple solution
            // to a linear equation. Note that we can't get a division by zero here -
            // if yp1 == yp0 then the above if be false.
            let cross = (xp1 - xp0) * (point.y - yp0) / (yp1 - yp0) + xp0

            // Finally check if it crosses to the left of our test point. You could equally
            // do right and it should give the same result.
            if cross < point.x {
                isInsidePolygon = !isInsidePolygon
            }
        }
    }

    return isInsidePolygon
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-19
    • 1970-01-01
    • 1970-01-01
    • 2019-05-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多