Question

How can i check if a pin is in viewable region of the map (MKMapView)?

Was it helpful?

Solution

A pin is a MKPinAnnotationView, it extends from MKAnnotationView and has a property annotation (that conforms to the protocol MKAnnotation). Such annotation has itself another property coordinate.

Just compare the latitude / longitude of such coordinate to the region of your map.

something like this should do it :

double minLong = myMap.region.center.longitude - myMap.region.span.longitudeDelta/2.0;
double maxLong = myMap.region.center.longitude + myMap.region.span.longitudeDelta/2.0;
double minLat = myMap.region.center.latitude - myMap.region.span.latitudeDelta/2.0;
double maxLat = myMap.region.center.latitude + myMap.region.span.latitudeDelta/2.0;

BOOL isPinInRegion = myPinCoordinates.longitude>=minLong && myPinCoordinates.longitude<=maxLong && myPinCoordinates.latitude>=minLat && myPinCoordinates.latitude<=maxLat;

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top