Created
February 13, 2013 09:38
-
-
Save swissmanu/4943356 to your computer and use it in GitHub Desktop.
check if a CLLocationCoordinate2D is inside MKCoordinateRegion. this is a simplified version of http://stackoverflow.com/a/10574327/368959
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-(BOOL)isCoordinate:(CLLocationCoordinate2D)coordinate insideRegion:(MKCoordinateRegion)region { | |
CLLocationCoordinate2D center = region.center; | |
CLLocationCoordinate2D northWestCorner, southEastCorner; | |
northWestCorner.latitude = center.latitude - (region.span.latitudeDelta / 2.0); | |
northWestCorner.longitude = center.longitude - (region.span.longitudeDelta / 2.0); | |
southEastCorner.latitude = center.latitude + (region.span.latitudeDelta / 2.0); | |
southEastCorner.longitude = center.longitude + (region.span.longitudeDelta / 2.0); | |
return(coordinate.latitude >= northWestCorner.latitude && | |
coordinate.latitude <= southEastCorner.latitude && | |
coordinate.longitude >= northWestCorner.longitude && | |
coordinate.longitude <= southEastCorner.longitude | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See Owen Godfrey's later comment (Oct 22) in this same stackoverflow thread:
"The accepted answer is a little verbose, and fails near the international dateline. "
He proposes an alternative in both Swift and Objective-C. See this at the (current) end of the thread.