Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.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
Swift2 Swift 2-用户';的当前位置在MKPolygon内_Swift2_Mapkit_Core Location_Cgpath_Mkpolygon - Fatal编程技术网

Swift2 Swift 2-用户';的当前位置在MKPolygon内

Swift2 Swift 2-用户';的当前位置在MKPolygon内,swift2,mapkit,core-location,cgpath,mkpolygon,Swift2,Mapkit,Core Location,Cgpath,Mkpolygon,我试图检查用户的当前位置是否在MKPolygon中。我已经创建了以下函数,但是对于我的所有测试用例,它都返回false。我有什么地方做错了吗 func isCorrectRegion(coordinates: Array<JSON>, userLocation: CLLocationCoordinate2D) -> Bool { var newCoordinates: [CLLocationCoordinate2D] = [] var mapPoints: [M

我试图检查用户的当前位置是否在MKPolygon中。我已经创建了以下函数,但是对于我的所有测试用例,它都返回false。我有什么地方做错了吗

func isCorrectRegion(coordinates: Array<JSON>, userLocation: CLLocationCoordinate2D) -> Bool {
    var newCoordinates: [CLLocationCoordinate2D] = []
    var mapPoints: [MKMapPoint] = []

    for(a):(JSON) in coordinates {
        let lat = a.arrayValue[1].double
        let long = a.arrayValue[0].double
        let location = CLLocationCoordinate2D(latitude: lat!, longitude: long!)
        newCoordinates.append(location)
    }

    for b in newCoordinates {
        let c: MKMapPoint = MKMapPointForCoordinate(b)
        mapPoints.append(c)
    }

    let polygon: MKPolygon = MKPolygon(points: &mapPoints, count: mapPoints.count)
    let polyRender: MKPolygonRenderer = MKPolygonRenderer(polygon: polygon)
    polyRender.invalidatePath()
    let target: MKMapPoint = MKMapPointForCoordinate(userLocation)
    let cgTarget: CGPoint = CGPoint(x: target.x, y: target.y)
    let isWithin = CGPathContainsPoint(polyRender.path, nil, cgTarget, false)

    return isWithin
}
func isCorrectRegion(坐标:数组,用户位置:CLLocationCoordinate2D)->Bool{
var newCoordinates:[CLLocationCoordinate2D]=[]
变量映射点:[MKMapPoint]=[]
对于(a):(JSON)在坐标中{
设lat=a.arrayValue[1]。双精度
设long=a.arrayValue[0]。双精度
let location=CLLocationCoordinate2D(纬度:纬度,经度:长!)
newCoordinates.append(位置)
}
在新坐标系中的b{
设c:MKMapPoint=mkmappointforcoordination(b)
mapPoints.append(c)
}
让多边形:MKPolygon=MKPolygon(点:&mapPoints,计数:mapPoints.count)
让polyRender:MKPolygonRenderer=MKPolygonRenderer(多边形:多边形)
polyRender.invalidatePath()
让目标:MKMapPoint=MKMapPointForCoordinate(userLocation)
设cgTarget:CGPoint=CGPoint(x:target.x,y:target.y)
让isWithin=CGPathContainsPoint(polyRender.path,nil,cgTarget,false)
返回时间在
}

在尝试了一些不同的方法后,终于找到了答案。希望这能帮助其他人:

func isCorrectRegion(coordinates: Array<JSON>, userLocation: CLLocationCoordinate2D) -> Bool {
    var newCoordinates: [CLLocationCoordinate2D] = []
    var mapPoints: [MKMapPoint] = []
    let mpr: CGMutablePathRef = CGPathCreateMutable()

    for(a):(JSON) in coordinates {
        let lat = a.arrayValue[1].double
        let long = a.arrayValue[0].double
        let location = CLLocationCoordinate2D(latitude: lat!, longitude: long!)
        newCoordinates.append(location)
    }

    for b in newCoordinates {
        let c: MKMapPoint = MKMapPointForCoordinate(b)
        mapPoints.append(c)
    }

    for var p = 0; p<mapPoints.count; p++ {
        if p == 0 {
            CGPathMoveToPoint(mpr, nil, CGFloat(mapPoints[p].x), CGFloat(mapPoints[p].y))
        } else {
            CGPathAddLineToPoint(mpr, nil, CGFloat(mapPoints[p].x), CGFloat(mapPoints[p].y))
        }
    }

    let target: MKMapPoint = MKMapPointForCoordinate(userLocation)
    let cgTarget: CGPoint = CGPoint(x: target.x, y: target.y)
    let isWithin = CGPathContainsPoint(mpr, nil, cgTarget, false)

    return isWithin
}
func isCorrectRegion(坐标:数组,用户位置:CLLocationCoordinate2D)->Bool{
var newCoordinates:[CLLocationCoordinate2D]=[]
变量映射点:[MKMapPoint]=[]
让mpr:CGMutablePathRef=CGPathCreateMutable()
对于(a):(JSON)在坐标中{
设lat=a.arrayValue[1]。双精度
设long=a.arrayValue[0]。双精度
let location=CLLocationCoordinate2D(纬度:纬度,经度:长!)
newCoordinates.append(位置)
}
在新坐标系中的b{
设c:MKMapPoint=mkmappointforcoordination(b)
mapPoints.append(c)
}
对于var p=0;p