问题
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?
回答1:
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.
来源:https://stackoverflow.com/questions/7886750/making-map-pins-spread-out-from-a-cluster