iOS - MKMapView 仅显示特定缩放级别的注释

发布于 2024-10-22 02:19:56 字数 87 浏览 0 评论 0原文

我有一个带有一些自定义注释的 MKMapView,当地图缩小得很远时,这些注释看起来不太好。

是否可以仅在地图处于特定缩放级别时显示/添加它们?

I have a MKMapView with some custom annotations that don't look that great when the map is zoom far out.

Is it possible to only show/add them when the map is at a certain zoom level?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

虐人心 2024-10-29 02:19:56

使用马科的回答我得出了这个解决方案。

每次区域更改时,我都会更改 ViewController 的属性 isAtBigZoom

func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
    isAtBigZoom = mapView.region.span.latitudeDelta < 0.01
}

然后在属性的 didSet 处,我执行此代码。

var isAtBigZoom = false {
    didSet {
        // this guard ensures, that the showing and hiding happens only once
        guard oldValue != isAtBigZoom else {
            return 
        }

        // in my case I wanted to show/hide only a certain type of annotations
        for case let annot as MapTextAnnotation in mapView.annotations {
            mapView.viewForAnnotation(annot)?.alpha = isAtBigZoom ? 1 : 0
        }
    }
}

如果您还想从隐藏注释开始,只需将 alpha 更改代码添加到 viewForAnnotation 方法中即可。

效果很好,我没有注意到性能方​​面的大问题。尽管这可能会随着注释数量的增加而改变......

Using Marko's answer I came to this solution.

Everytime region changes, I change the ViewController's property isAtBigZoom.

func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
    isAtBigZoom = mapView.region.span.latitudeDelta < 0.01
}

Then at didSet of the property, I execute this code.

var isAtBigZoom = false {
    didSet {
        // this guard ensures, that the showing and hiding happens only once
        guard oldValue != isAtBigZoom else {
            return 
        }

        // in my case I wanted to show/hide only a certain type of annotations
        for case let annot as MapTextAnnotation in mapView.annotations {
            mapView.viewForAnnotation(annot)?.alpha = isAtBigZoom ? 1 : 0
        }
    }
}

If you also want to start with the annotations hidden, just add the alpha changing code to viewForAnnotation method.

Works great and I haven't noticed big issues with performance. Though that may change with the increasing number of annotations...

猫性小仙女 2024-10-29 02:19:56

获取地图缩放级别

[map region];

您可以通过MKMapView 的属性 。您还会收到区域更改事件的通知
通过实现 MKMapViewDelegate 方法并在此处设置委托

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated

,您可以检查当前的缩放级别。我不建议在缩放/平移时删除或添加所有注释,因为这确实会影响应用程序的性能。我还没有真正尝试过将 alpha 设置为 0.0 或 MKAnnotationView 上的隐藏属性,但这可能是您最好的选择。

You can get the map zoom level via

[map region];

property of the MKMapView. also you get the notifications for region changing events
by implementing the MKMapViewDelegate method and setting the delegate

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated

here you can check what your current zoom level is. I don't recommend removing or adding all the annotations while zooming / panning since that could really effect the app performance. I haven't really tried setting alpha to 0.0 or hidden property on MKAnnotationView, but that could be your best bet.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文