Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用CLLocationCoordinate2D的矩形_Ios_Swift_Coordinates_Core Location - Fatal编程技术网

Ios 使用CLLocationCoordinate2D的矩形

Ios 使用CLLocationCoordinate2D的矩形,ios,swift,coordinates,core-location,Ios,Swift,Coordinates,Core Location,我想知道是否可以使用左上角和右下角的CLLocationCoordinate2D创建一个矩形,然后通过检查(topLeftCoordinate.latitudecoordinate.latitude)来检查坐标是否是该矩形的一部分(topLeftCoordinate.longitudecoordinate.longitude) 我原以为坐标代表球体上的一个点,但这不起作用。但由于CLLocationCoordinate2D末尾的2D,我感到困惑。有人能澄清一下吗 谢谢:)您将能够根据坐标创建一个

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

我原以为坐标代表球体上的一个点,但这不起作用。但由于
CLLocationCoordinate2D
末尾的
2D
,我感到困惑。有人能澄清一下吗


谢谢:)

您将能够根据坐标创建一个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
}
func isPoint(点:MKMapPoint,内多边形poly:MKPolygon)->Bool{
设多边形垂直=多边形点()
var isInsidePolygon=false

对于0中的i..您将能够从中找到您要查找的内容。