使地图图钉从群集中散布 [英] Making map pins spread out from a cluster

查看:73
本文介绍了使地图图钉从群集中散布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的地图有两个自定义注释类:一个用于与位置绑定的单个帖子,另一个用于这些帖子的群集.集群存储指向它包含的所有帖子的指针以及中心纬度/经度位置(使用它包含的帖子的位置来计算).我的行为是,当我单击集群注释时,它将删除集群并将其帖子添加到地图.我想要的是在将群集扩展为动画时更改图钉放置注释,从而使新图钉从群集的中心向外移动到其新位置.但是,我也有一些帖子由于它们与其他点之间的距离而从未聚集.显然,他们没有此动画,因为没有相关的位置可以向外移动.有人知道我该怎么实现吗?

I've got two custom annotation classes for my map: one for a single post tied to a location, and one for a cluster of those posts. The cluster stores pointers to all of the posts it contains, as well as a central lat/long location (calculated using the locations of the posts it contains). I have the behaviour that when I click on a cluster annotation it removes the cluster and adds its posts to the map. What I want is to change the pin-drop annotation when expanding the clusters to an animation whereby the new pins move outwards from the centre of the cluster to their new locations. However, I also have some posts that are never clustered due to their distance from other points. Obviously they can't have this animation as there is no associated location for them to move outward from. Does anyone know how I might implement this?

推荐答案

使插针从群集中心扩展实际上很容易.制作新的单针脚注解时,将其坐标设置为群集中心:

Making the pins expand from the cluster center is actually pretty easy. When you make the new single-pin annotations, set their coordinates to the cluster center:

id <MKAnnotation> pin;
CLLocationCoordinate2D clusterCenter;
// ...
pin.coordinate = clusterCenter;

viewForAnnotation:中,请勿为新的图钉设置动画:

In viewForAnnotation:, don't animate the new pins:

MKPinAnnotationView *pinView;
// ...
pinView.animatesDrop = NO;

然后,在将图钉添加到地图视图之后,您将对其进行动画处理以将其移动到它们的实际位置:

Then, after you've added the pins to the map view, you'll animate moving them to their real positions:

MKMapView *mapView;
id <MKAnnotation> pin;
// ...
// probably loop over annotations
[mapView addAnnotation:pin];
NSTimeInterval interval = 1.0; // or whatever
[UIView animateWithDuration:interval animations:^{
    // probably loop over annotations here again
    CLLocationCoordinate2D realCoord;
    // ...
    pin.coordinate = realCoord;
}];

对于非聚集引脚的问题,如果不详细了解实现方式,很难回答,但是我认为存在很多可能性.您可能只有一个跳过动画的简单标志.或者,您可以将它们完全相同地对待,即使它们是单独的,也仍然可以聚类"它们,并且仍然可以对它们进行动画处理……虽然效率不高,但是可以正常工作,并且您的代码会更简洁.

As for the problem of non-clustered pins, that's harder to answer without knowing the implementation in detail, but I think there are lots of possibilities. You could just have a simple flag that skips the animation. Or you could just treat them exactly the same, and still "cluster" them even when they're solo, and still animate them ... not maximally efficient, but it would work and your code would be cleaner.

这篇关于使地图图钉从群集中散布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆