按纬度排序MKAnnotationViews数组?

时间:2015-03-19 14:07:32

标签: ios nsarray mapkit coordinates mkannotationview

根据一些动态标准,我在mapkit中注释各种坐标并按如下方式设置它们的动画:

-(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;
        }
    }

我想要做的是在动画制作之前订购annotationViews数组。具体来说,我想访问每个annotationView的坐标:

CLLocationCoordinate2D x = annView.annotation.coordinate;

按纬度排序。最终的效果应该是它们从西向东起波动。

是否可以按纬度排序?

1 个答案:

答案 0 :(得分:1)

首先创建一个新的视图排序数组,然后循环遍历排序数组。

但是如果你想“从 west east 的波纹”,那么注释应该按经度排序(不是< EM>纬度)。

示例:

-(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;
    }
}