如何在不影响MKMapView性能的情况下放置MKAnnotation

时间:2011-07-04 11:15:01

标签: objective-c ios xcode mkmapview mkannotation

我的地图上有超过2800个地点。 但是当我把它们放在地图上时,地图就会冻结。我什么都不做,只能等到所有注释数据都可用。

    - (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{
        NSAutoreleasePool *pool_mr = [[NSAutoreleasePool alloc] init];

        NSLog(@"mapView:regionDidChangeAnimated:");
        NSLog(@"latitude: %f, longitude: %f", regionsMapView.centerCoordinate.latitude, regionsMapView.centerCoordinate.longitude);
        NSLog(@"latitudeDelta: %f, longitudeDelta: %f", regionsMapView.region.span.latitudeDelta, regionsMapView.region.span.longitudeDelta);

        if (regionsMapView.region.span.latitudeDelta < 0.007) {
            NSLog(@"SHOW ANNOTATIONS");
            NSArray *annotations = [regionsMapView annotations];  
            AddressAnnotation *annotation = nil; 
            for (int i=0; i<[annotations count]; i++)
            {
                NSLog(@"%i", i);
                annotation = (AddressAnnotation*)[annotations objectAtIndex:i];
                [[regionsMapView viewForAnnotation:annotation] setHidden:NO];
            }
        }else {
            NSLog(@"HIDE ANNOTATIONS");
            NSArray *annotations = [regionsMapView annotations];  
            AddressAnnotation *annotation = nil; 
            for (int i=0; i<[annotations count]; i++)
            {
                NSLog(@"%i", i);
                annotation = (AddressAnnotation*)[annotations objectAtIndex:i];
                [[regionsMapView viewForAnnotation:annotation] setHidden:YES];
            }

        }
    [pool_mr release];
}

另一种方法如下:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation, AddressAnnotation>) annotation {    
    //NSAutoreleasePool *pool5 = [[NSAutoreleasePool alloc] init];
    // if it's the user location, just return nil.
    if ([annotation isKindOfClass:[MKUserLocation class]]){
        NSLog(@"MKUserLocation");
        return nil;
    }
    else {
        NSLog(@"mapView:viewForAnnotation>>>");
        NSLog(@"%@", [annotation markerColor]);
        NSLog(@"image: %@", [NSMutableString stringWithFormat:@"MKPinAnnotationView_%@.png",[annotation markerColor]]);
        MKPinAnnotationView *annView=[[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:[annotation markerColor]] autorelease];
        //annView.pinColor = MKPinAnnotationColorPurple;
        UIImage *annotationImage = [[UIImage imageNamed:[NSMutableString stringWithFormat:@"MKPinAnnotationView_%@.png",[annotation markerColor]]] autorelease];

        annView.image = annotationImage;
        annView.animatesDrop = NO;
        annView.canShowCallout = YES;
        //annView.draggable = NO;
        //annView.highlighted = NO;

        annView.calloutOffset = CGPointMake(-5, 5);
        return annView;
    }

    //[pool5 release];
    NSLog(@"<<<mapView:viewForAnnotation");

    return nil;

}

1 个答案:

答案 0 :(得分:4)

使用这么多注释,我会将它们聚类,以便在放大时获得更多细节。我在地图上放了几千个这样的东西,它起作用了。如果您搜索地图引脚群集,有很多信息。

例如,这里有一个commercial option(没有连接,没有使用过)但是如果你环顾四周,你会看到很多关于如何自己实现它的信息。