Skip to content

Instantly share code, notes, and snippets.

@swissmanu
Created February 13, 2013 09:38
Show Gist options
  • Save swissmanu/4943356 to your computer and use it in GitHub Desktop.
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
-(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
);
}
@SteveCaine
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment