There are annotations (visual tags) on a map. When the user scales (zooms in or out) in the map a method named 'regionDidChangeAnimated' is called. Every time this is done I try to use fast enumeration to see which annotation objects currently intersect which ones on the map. This is because I'm using YandexMapKit (not apples standard MapKit) and there isn't much of any kind of documentation or examples on how this can be done otherwise. Here's how the only code I could come up with so far, but it ends up crashing the app:
NSArray *allAnnotations = [_mapView annotations];
// Check for intersecting annotation bounds.
for (RBMapAnnotation *annotation1 in allAnnotations)
{
YMKPinAnnotationView *pinView1 = (YMKPinAnnotationView *)annotation1; // Still returns as 'RBMapAnnotation'.
for (RBMapAnnotation *annotation2 in allAnnotations)
{
YMKPinAnnotationView *pinView2 = (YMKPinAnnotationView *)annotation2;
if (CGRectIntersectsRect([pinView1 convertRect:pinView1.bounds toView:nil], [pinView2 convertRect:pinView2.bounds toView:nil]))
{
NSLog(@"Interesction between %@ and %@", annotation1.offer.name, annotation2.offer.name);
//[annotationsToUpdate addObject:annotation];
}
}
}
Please help me out with this. What I am trying to attain is if any annotations are intersecting on the map visually, then I will remove them both and substitute them with 1 annotation.
Ignoring algorithmic inefficiency (you might want to rework this so it isn't O(N^2))…
You mention that the code crashes, but have not provided any diagnostics/backtrace to assist in diagnosing the crash.
You are looping over the annotations, casting them to YMKPinAnnotationView
, and them treating them as such. It is likely that this is the source of your crash. It appears that the annotations are instances which conform to YMKAnnotation
, but are not in fact views. You should send the map view -viewForAnnotation:
passing the annotation to get the associated view.