【问题标题】:ios: predict if two annotation views would overlap each otherios:预测两个注释视图是否会相互重叠
【发布时间】:2014-10-23 09:52:50
【问题描述】:

我正在开发一些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;
}

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

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

【问题讨论】:

  • 存在 OCMapViewkingpin 等库。这些可能不是最好的,但看看谷歌,你会找到适合你需要的东西。
  • 什么是 CoordinatesAlert ?
  • @DavidAnsermot 只是一些包含显示位置的模型
  • @rdurand 是的,我将检查这些来源,但我认为库对于我的目的来说不是那么灵活,也许我将无法自定义它们。 UI 将过于复杂,以不同方式显示的自定义标注视图取决于注释计数和其他因素

标签: ios objective-c iphone mkannotation mkannotationview


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2010-09-23
    • 1970-01-01
    • 2021-01-27
    • 1970-01-01
    • 2011-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多