Search code examples
iosobjective-ciphonemkannotationmkannotationview

ios: predict if two annotation views would overlap each other


I'm developing some MKMapView logic. There are many annotations on my map view. I need to compose few locations in one if those annotation views would overlap each other, and display Annotation view with changes. So I should predict that case, and I need to determinate that only from annotations' location properties and current MKMapView's zoom.

- (BOOL)shouldAlert:(CoordinatesAlert *)alert1 beInGroupWithAlert:(CoordinatesAlert *)alert2 {
    // somehow calculate current map view zoom
    CGFloat zoom = ...
    if ([alert1.location distanceFromLocation:alert2.location] / zoom < kSomeCoeficient) {
        return YES;
    }
    return NO;
}

Also I worried about this solution because I need to reload annotations and those views on every zoom change.

How can I do this in better way? Is there any default solution for annotation views clustering?


Solution

  • All of exist libraries do not suit for my needs, they all works with areas but I need to group annotations only if they would be overlapped. Also I do not need so many annotations, so i do not need performance optimization. So, I've found some solution, it is better then nothing.

    - (BOOL)isViewForLocation:(CLLocation *)location1 overlappedByViewForLocation:(CLLocation *)location2 {
        const CLLocationDegrees deltaLatitude = ABS(location1.coordinate.latitude - location1.coordinate.latitude);
        const CLLocationDegrees deltaLongitude = ABS(location2.coordinate.longitude - location2.coordinate.longitude);
        const CLLocationDegrees mapAreaLatitude = self.mapView.region.span.latitudeDelta;
        const CLLocationDegrees mapAreaLongitude = self.mapView.region.span.longitudeDelta;
    
        return (deltaLatitude / mapAreaLatitude) < (kAnnotationTapArea.size.height / self.mapView.frame.size.height)
        && (deltaLongitude / mapAreaLongitude) < (kAnnotationTapArea.size.width / self.mapView.frame.size.width);
    }