Justin Moore
Justin Moore

Reputation: 884

Order array of MKAnnotationViews by latitude?

Based on some dynamic criteria, I am annotating various coordinates in mapkit and animating them as follows:

-(void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)annotationViews {

        float delay = 0;
        for (MKAnnotationView *annView in annotationViews) {

            annView.transform = CGAffineTransformMakeScale(0.01, 0.01);
            CLLocationCoordinate2D x = annView.annotation.coordinate;
            float y = x.latitude;
            [UIView animateWithDuration:.2
                                  delay:delay
                 usingSpringWithDamping:damp
                  initialSpringVelocity:vel
                                options:0
                             animations:^{
                                 annView.transform = CGAffineTransformIdentity;
                             } completion:^(BOOL finished) {

                             }];
            delay += .02;
        }
    }

What I would like to do is order the array of annotationViews prior to animating them. Specifically, I'd like to access the coordinates of each annotationView:

CLLocationCoordinate2D x = annView.annotation.coordinate;

and order them by latitude. The final effect should be that they animate in a ripple from west to east.

Is ordering them by latitude possible?

Upvotes: 1

Views: 48

Answers (1)

user467105
user467105

Reputation:

First create a new sorted array of the views and then loop through the sorted array.

But if you want to "animate in a ripple from west to east" then the annotations should be sorted by longitude (not latitude).

Example:

-(void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)annotationViews {

    NSArray *sortedViews = [annotationViews sortedArrayUsingComparator:
        ^NSComparisonResult(MKAnnotationView *obj1, MKAnnotationView *obj2) {
            if (obj1.annotation.coordinate.longitude < obj2.annotation.coordinate.longitude)
                return NSOrderedAscending;
            else
                if (obj1.annotation.coordinate.longitude > obj2.annotation.coordinate.longitude)
                    return NSOrderedDescending;
                else
                    return NSOrderedSame;
        }];


    float delay = 0;
    for (MKAnnotationView *annView in sortedViews) {

        annView.transform = CGAffineTransformMakeScale(0.01, 0.01);

        [UIView animateWithDuration:.2
                              delay:delay
             usingSpringWithDamping:damp
              initialSpringVelocity:vel
                            options:0
                         animations:^{
                             annView.transform = CGAffineTransformIdentity;
                         } completion:^(BOOL finished) {

                         }];
        delay += .02;
    }
}

Upvotes: 1

Related Questions