Swift 2 - User's Current Location is inside MKPolygon

samk

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

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
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to get user's current location in Android

From Dev

How to get user's current location in Android

From Dev

select2: Check current user location

From Dev

Query a location database based on the user's current location

From Dev

Distance of user's location from my current location

From Dev

Swift2 iOS -Logging a current user out and creating a new user keeps the old user's defaults for the new user

From Dev

How do I check a user's current location?

From Dev

iOS mapKit refresh direction when the user's current location is updated

From Dev

how to add user's current location in ng-autocomplete

From Dev

How to track facebook current user's location IOS/Android

From Dev

Filter a database of locations due to user's current location with radius

From Dev

Swift Converting CGMutablePath to MKPolygon

From Dev

GoogleMap V2 doesn't start with user current location

From Dev

How to determine an Android device's current country location regardless of user location settings?

From Dev

Google Map centred on user's current location PLUS default location as a backup

From Dev

Validate if a latlong is inside a MKPolygon in iOS

From Dev

Getting user location in swift

From Dev

Getting user location in swift

From Dev

How to zoom to current user location?

From Dev

How I can center the map on user's location in swift?

From Dev

Swift 4, Xcode 9: Getting and displaying the user's location

From Dev

Show Current Location and Update Location in MKMapView in Swift

From Dev

Swift - Weekday by current location by currentCalendar()

From Dev

Find user's current location(latitude and longitude) when user logged in, through Facebook api?

From Dev

How to know if user rejected the Location Services in Swift 2

From Dev

Making the map zoom to user location and annotation (swift 2)

From Dev

Swift 2 Mapkit Get City from user location

From Dev

Swift 2 Mapkit Get City from user location

From Dev

Check if user location is inside a shape

Related Related

  1. 1

    How to get user's current location in Android

  2. 2

    How to get user's current location in Android

  3. 3

    select2: Check current user location

  4. 4

    Query a location database based on the user's current location

  5. 5

    Distance of user's location from my current location

  6. 6

    Swift2 iOS -Logging a current user out and creating a new user keeps the old user's defaults for the new user

  7. 7

    How do I check a user's current location?

  8. 8

    iOS mapKit refresh direction when the user's current location is updated

  9. 9

    how to add user's current location in ng-autocomplete

  10. 10

    How to track facebook current user's location IOS/Android

  11. 11

    Filter a database of locations due to user's current location with radius

  12. 12

    Swift Converting CGMutablePath to MKPolygon

  13. 13

    GoogleMap V2 doesn't start with user current location

  14. 14

    How to determine an Android device's current country location regardless of user location settings?

  15. 15

    Google Map centred on user's current location PLUS default location as a backup

  16. 16

    Validate if a latlong is inside a MKPolygon in iOS

  17. 17

    Getting user location in swift

  18. 18

    Getting user location in swift

  19. 19

    How to zoom to current user location?

  20. 20

    How I can center the map on user's location in swift?

  21. 21

    Swift 4, Xcode 9: Getting and displaying the user's location

  22. 22

    Show Current Location and Update Location in MKMapView in Swift

  23. 23

    Swift - Weekday by current location by currentCalendar()

  24. 24

    Find user's current location(latitude and longitude) when user logged in, through Facebook api?

  25. 25

    How to know if user rejected the Location Services in Swift 2

  26. 26

    Making the map zoom to user location and annotation (swift 2)

  27. 27

    Swift 2 Mapkit Get City from user location

  28. 28

    Swift 2 Mapkit Get City from user location

  29. 29

    Check if user location is inside a shape

HotTag

Archive