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

发布于 2024-11-18 12:07:04 字数 2936 浏览 3 评论 0原文

我的地图上有超过 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;

}

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;

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倚栏听风 2024-11-25 12:07:04

有了这么多的注释,我会将它们聚集起来,这样当你放大时,你就能获得更多细节。我用这种方法在地图上画了几千个,效果很好。如果你搜索的话,有很多关于地图图钉聚类的信息。

例如,这里有一个商业选项(没有连接,没有使用过),但是如果你环顾四周,你会看到很多有关如何自行实施的信息。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文