ios:预测两个注释视图是否会相互重叠

时间:2014-10-23 09:52:51

标签: ios objective-c iphone mkannotation mkannotationview

我正在开发一些MKMapView逻辑。我的地图视图上有许多注释。如果这些注释视图彼此重叠,我需要在一个中组合几个位置,并显示带有更改的注释视图。所以我应该预测这种情况,我需要确定只有注释'位置属性和当前MKMapView缩放。

- (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;
}

我也担心这个解决方案,因为我需要在每次缩放变化时重新加载注释和那些视图。

我怎样才能更好地做到这一点?注释视图群集是否有任何默认解决方案?

1 个答案:

答案 0 :(得分:0)

所有存在的库都不适合我的需求,它们都适用于区域,但只有在它们重叠时我才需要对注释进行分组。此外,我不需要这么多注释,所以我不需要性能优化。 所以,我找到了一些解决方案,它没有任何好处。

- (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);
}
相关问题