How to determine if the user's current location is inside my MKCoordinateRegion?

I have a coordinate area that I defined contains the limits of what I want to show for my application. I set this as MKCoordinateRegion with center lat point, longitude and range. How to determine if the current userLocation is inside my coordinate region?

+7
source share
3 answers

Use rectangular cards. Here's an example using the current current map view. As for your question, you can use convertRegion:toRectToView: to pre-convert your region to MKMapRect .

 MKMapPoint userPoint = MKMapPointForCoordinate(mapView.userLocation.location.coordinate); MKMapRect mapRect = mapView.visibleMapRect; BOOL inside = MKMapRectContainsPoint(mapRect, userPoint); 
+9
source

Swift 3 version of the answer firstresponder :

 let userPoint = MKMapPointForCoordinate(mapView.userLocation.coordinate) let mapRect = mapView.visibleMapRect let inside = MKMapRectContainsPoint(mapRect, userPoint) 

Pretty much the same thing. This API has not yet been Swift-ified (i.e., updated in accordance with the Swift API development guidelines). It really has to be ...

 let userPoint = mapView.userLocation.coordinate.mapPoint let inside = mapView.visibleMapRect.contains(userPoint) 
+2
source

There is a simple solution to decide if a point is inside your area if the area is set by a polygon using the ray casting algorithm: see here http://en.wikipedia.org/wiki/Point_in_polygon

As a starting point, use a location guaranteed outside of your region, for example. (geographic) north pole.

0
source

All Articles