OGeek|极客世界-中国程序员成长平台

标题: objective-c - 如何在不影响 MKMapView 性能的情况下放置 MKAnnotation [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 16:39
标题: objective-c - 如何在不影响 MKMapView 性能的情况下放置 MKAnnotation

我的 map 上有 2800 多个位置。 但是当我把它们放在 map 上时, map 是卡住的。我只能等到所有注释数据都可用。

    - (void)mapViewMKMapView *)mapView regionDidChangeAnimatedBOOL)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 *)mapViewMKMapView *)mapView viewForAnnotationid <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;

}



Best Answer-推荐答案


有了这么多注释,我会将它们聚集在一起,以便在放大时获得更多细节。我用这种方式在 map 上放了几千 block ,效果很好。如果你搜索它,有很多关于 map 针聚类的信息。

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

关于objective-c - 如何在不影响 MKMapView 性能的情况下放置 MKAnnotation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6570729/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4