Swift 3 / current CLLocation inside MKPolygon without CGPath or Mapview

234 views Asked by At

Full disclosure - I'm new here, but I've dug through everything and can't seem to parse together what I need. I'm at the point where Google is only returning purple links.

Is there a way to check user's current CLLocation lat/lon against several MKPolygon's without operating a MapView? Basically just in the background until lat/lon is inside MKPolygon then return a BOOL.

Here's what I have currently.

var points: [CLLocationCoordinate2D] = [CLLocationCoordinate2D]()

        points.append(CLLocationCoordinate2DMake(43.8138, -78.8884))
        points.append(CLLocationCoordinate2DMake(43.8074, -78.8768))
        points.append(CLLocationCoordinate2DMake(43.8130, -78.8688))
        points.append(CLLocationCoordinate2DMake(43.8210, -78.8806))

    let zoneA: MKPolygon = MKPolygon(coordinates: &points, count: 4)

    let point1 = CLLocationCoordinate2D(latitude: 43.8146, longitude: -78.8784)


    func contained(by vertices: [CLLocationCoordinate2D]) -> Bool {

        let path = CGMutablePath()

        for vertex in vertices {

            if path.isEmpty
            {
                path.move(to: CGPoint(x: vertex.longitude, y: vertex.latitude))
            }
            else
            {
                path.addLine(to: CGPoint(x: vertex.longitude, y: vertex.latitude))
            }
        }

        let point = CGPoint(x: 43.7146, y: -78.8784)

        return path.contains(point)
    }
0

There are 0 answers