Swift 2 - User's Current Location is inside MKPolygon

643 Views Asked by At

I am trying to check if the user's current location is inside the MKPolygon. I have created the following function but it returns false for all my test cases. Is there something I might be doing wrong?

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
}
1

There are 1 best solutions below

0
On BEST ANSWER

Finally figured it out after trying a few different things. Hope this helps others:

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
}