If I have a MKMapView and a CLLocationCoordinate2D how do you test whether the map\'s visible area contains the coordinate?
If you frequently work with maps I suggest you to create an extension like this:
extension MKMapView {
func contains(coordinate: CLLocationCoordinate2D) -> Bool {
return MKMapRectContainsPoint(self.visibleMapRect, MKMapPointForCoordinate(coordinate))
}
}
Then you can use wherever, for example:
func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
if mapView.contains(coordinate: mapView.centerCoordinate) {
// do stuff
}
}
In this way you keep the code: