简体   繁体   中英

how to put MKAnnotation without affecting the MKMapView performance

I have more than 2800 locations to be put on my map. But when i am putting them the map is freezing. i can do nothing but wait until all the annotation data is available.

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

And the Another method is like below:

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

}

With that number of annotations, I would cluster them so as you zoom in, you get more detail. I put several thousand on a map that way and it works a treat. There is lots of information about map pin clustering if you search for it.

Here's one commercial option for example (no connection, haven't used) but if you look around you'll see plenty of information on how to implement it yourself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM