Last active
May 19, 2018 16:04
-
-
Save camdeardorff/925a3f90714c7f03d30960cc00aaaf8b to your computer and use it in GitHub Desktop.
An extension to the MKMapView class with an function to determine whether a region is valid and won't cause a crash if placed on a map.
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
// to solve the invalid region <center:##.###, ##.### span:##.###, ##.###> issue | |
extension MKMapView { | |
// determines if the region provided is in fact a valid region that can be shown on the map | |
// notes: latitude -> [-90, 90], !nan, !inf | |
// longitude -> [-180, 180], !nan, !inf | |
// latitudeDelta -> [0, 180], !nan, !inf | |
// longitudeDelta -> [0, 360], !nan, !inf | |
func canShow(region: MKCoordinateRegion) -> Bool { | |
//.....center latitude | |
return region.center.latitude != Double.infinity && region.center.latitude != Double.nan | |
&& region.center.latitude >= -90 && region.center.latitude <= 90 | |
// center longitude | |
&& region.center.longitude != Double.infinity && region.center.longitude != Double.nan | |
&& region.center.longitude >= -180 && region.center.longitude <= 180 | |
// span latitude delta | |
&& region.span.latitudeDelta != Double.infinity && region.span.latitudeDelta != Double.nan | |
&& region.span.latitudeDelta >= 0 && region.span.latitudeDelta <= 180 | |
// span longitude delta | |
&& region.span.longitudeDelta != Double.infinity && region.span.longitudeDelta != Double.nan | |
&& region.span.longitudeDelta >= 0 && region.span.longitudeDelta <= 360 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment