Making map pins spread out from a cluster

↘锁芯ラ 提交于 2019-12-04 14:29:56

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;

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!