添加多个注释iPhone MapView类是缓慢的 [英] Adding multiple annotations to iPhone MapView is slow

查看:190
本文介绍了添加多个注释iPhone MapView类是缓慢的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有code,需要一个标题和一个位置的地址,然后得到谷歌地图坐标CSV文件,然后添加注释。它的工作原理,但是当我想一次冻结约30-40秒加50个或更多注释,然后显示批注,所以我想知道的是为什么我搞砸了内存管理,这就是为什么它这么慢,什么其他?我可以加快步伐?

下面是code:

   - (无效)addAnnotation:(* NSString的)annoTitle:(* NSString的)annoAddress
{
    * NSString的地址= annoAddress;    * NSString的urlString = [的NSString stringWithFormat:@http://maps.google.com/maps/geo?q=%@&output=csv,[地址stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    的NSString * locationString = [NSString的stringWithContentsOfURL:[NSURL URLWithString:urlString]];
    NSArray的*时listItems = [locationString componentsSeparatedByString:@];    浮langcoord = [[listItems中objectAtIndex:2]的floatValue];
    浮longcoord = [[listItems中objectAtIndex:3]的floatValue];    CLLocationCoordinate2D坐标= CLLocationCoordinate2DMake(langcoord,longcoord);    //与当前数据创建的MapPoint的一个实例
    注释*注释= [[译注页头] initWithCoordinate:坐标标题:annoTitle];    //将它添加到地图视图
    [图形页面addAnnotation:注释]。    //的MKMapView保留了注释,所以我们可以释放它
    [注释发布]
}


解决方案

下面的方法使用了上MapKit多插针。

   - (无效)viewWillAppear中:动画(BOOL){    NSArray的* AS = [NSArray的arrayWithArray:[报告objectAtIndex:0];
    的for(int i = 0; I< AS计数];我++){
            将*家= [[[广场ALLOC] INIT]自动释放];
home.name = [[AS objectAtIndex:我] valueForKey:@意见];
            home.latitude = [[[AS objectAtIndex:我] valueForKey:@纬度]的floatValue];
            home.longitude = [[[AS objectAtIndex:我] valueForKey:@经度]的floatValue];
valueForKey:@纬度]的floatValue],[[[AS objectAtIndex:ⅰ] valueForKey:@经度]的floatValue]];            标*从= [[[标页头] initWithPlace:家居】自动释放];
            [图形页面addAnnotation:来自];
}
    [个体经营centerMap]    [UIApplication的sharedApplication] .networkActivityIndi​​catorVisible = NO;
}

集中式地图两个引脚之间

   - (无效)centerMap
{
    MKCoordinateRegion区域;
    CLLocationDegrees maxLat = -90;
    CLLocationDegrees maxLon = -180;
    CLLocationDegrees minLat = 120;
    CLLocationDegrees MINLON = 150;
    NSArray的* TEMP = [NSArray的arrayWithArray:[NSArray的arrayWithArray:[报告objectAtIndex:0]]];
    的for(int i = 0; I<温度计数];我++){
        将*家= [[[广场ALLOC] INIT]自动释放];
        home.latitude = [[[温度objectAtIndex:我] valueForKey:@纬度]的floatValue];
        home.longitude = [[[温度objectAtIndex:我] valueForKey:@经度]的floatValue];        标*从= [[[标页头] initWithPlace:家居】自动释放];        CLLocation * currentLocation =(CLLocation *)FROM;
        如果(currentLocation.coordinate.latitude> maxLat)
            maxLat = currentLocation.coordinate.latitude;
        如果(currentLocation.coordinate.latitude< minLat)
            minLat = currentLocation.coordinate.latitude;
        如果(currentLocation.coordinate.longitude> maxLon)
            maxLon = currentLocation.coordinate.longitude;
        如果(currentLocation.coordinate.longitude< MINLON)
            MINLON = currentLocation.coordinate.longitude;        region.center.latitude =(maxLat + minLat)/ 2;
        region.center.longitude =(maxLon + MINLON)/ 2;
        region.span.latitudeDelta = maxLat - minLat;
        region.span.longitudeDelta = maxLon - MINLON;
    }
    [图形页面setRegion:地区动画:是];
}

在这里定义多个注释。

   - (MKAnnotationView *)的MapView:(*的MKMapView)的地图viewForAnnotation:(ID< MKAnnotation>)注释
{    如果(注释== mapView.userLocation)
        回零;    MKPinAnnotationView *销=(MKPinAnnotationView *)[图形页面dequeueReusableAnnotationViewWithIdentifier:@ASDF];    如果(PIN ==无)
        引脚= [[[MKPinAnnotationView页头] initWithAnnotation:注释reuseIdentifier:@ASDF]自动释放];
    其他
        pin.annotation =注释;
        pin.userInteractionEnabled = YES;
        *的UIButton = disclosureButton [UIButton的buttonWithType:UIButtonTypeDetailDisclosure];
        [disclosureButton SETFRAME:CGRectMake(0,0,30,30)];        pin.rightCalloutAccessoryView = disclosureButton;
        pin.pinColor = MKPinAnnotationColorRed;
        pin.animatesDrop = YES;
        [销的setEnabled:YES];
        [销setCanShowCallout:YES];
        回复销;
}

更多细节及在这里下载示例code。

I have code that takes a title and an address of a location,then get coordinates from google maps csv, then add annotation. It works, but when I want to add 50 or more annotation at once it freezes for about 30-40 seconds and then displays annotations, so what I want to know is did I screw up memory management and that's why it's so slow, or something else? Can I speed it up?

Here is the code:

- (void) addAnnotation: (NSString*) annoTitle: (NSString*) annoAddress
{
    NSString *address = annoAddress;

    NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv", [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString]];
    NSArray *listItems = [locationString componentsSeparatedByString:@","];

    float langcoord = [[listItems objectAtIndex:2] floatValue];
    float longcoord = [[listItems objectAtIndex:3] floatValue];

    CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(langcoord, longcoord);

    // Create an instance of MapPoint with the current data
    Annotation *annotation = [[Annotation alloc] initWithCoordinate:coord title:annoTitle];

    // Add it to the map view
    [mapView addAnnotation:annotation];

    // MKMapView retains its annotations, so we can release it
    [annotation release];
}

解决方案

Following methods are use for the multiple pin on MapKit.

-(void)viewWillAppear:(BOOL)animated {

    NSArray *AS=[NSArray arrayWithArray:[reports objectAtIndex:0]];
    for (int i=0; i<[AS count]; i++) {
            Place* home = [[[Place alloc] init] autorelease];
home.name = [[AS objectAtIndex:i] valueForKey:@"comments"];
            home.latitude = [[[AS objectAtIndex:i] valueForKey:@"latitude"]floatValue];
            home.longitude = [[[AS objectAtIndex:i] valueForKey:@"longitude"]floatValue];
valueForKey:@"latitude"]floatValue],[[[AS objectAtIndex:i] valueForKey:@"longitude"]floatValue]];

            PlaceMark* from = [[[PlaceMark alloc] initWithPlace:home] autorelease];
            [mapView addAnnotation:from];
}
    [self centerMap];

    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}

Centralized Map between two pins

-(void) centerMap
{
    MKCoordinateRegion region;
    CLLocationDegrees maxLat = -90;
    CLLocationDegrees maxLon = -180;
    CLLocationDegrees minLat = 120;
    CLLocationDegrees minLon = 150;
    NSArray *temp=[NSArray arrayWithArray:[NSArray arrayWithArray:[reports objectAtIndex:0]]];
    for (int i=0; i<[temp count];i++) {
        Place* home = [[[Place alloc] init] autorelease];
        home.latitude = [[[temp objectAtIndex:i] valueForKey:@"latitude"]floatValue];
        home.longitude =[[[temp objectAtIndex:i] valueForKey:@"longitude"]floatValue];

        PlaceMark* from = [[[PlaceMark alloc] initWithPlace:home] autorelease];

        CLLocation* currentLocation = (CLLocation*)from ;
        if(currentLocation.coordinate.latitude > maxLat)
            maxLat = currentLocation.coordinate.latitude;
        if(currentLocation.coordinate.latitude < minLat)
            minLat = currentLocation.coordinate.latitude;
        if(currentLocation.coordinate.longitude > maxLon)
            maxLon = currentLocation.coordinate.longitude;
        if(currentLocation.coordinate.longitude < minLon)
            minLon = currentLocation.coordinate.longitude;

        region.center.latitude     = (maxLat + minLat) / 2;
        region.center.longitude    = (maxLon + minLon) / 2;
        region.span.latitudeDelta  =  maxLat - minLat;
        region.span.longitudeDelta = maxLon - minLon;
    }
    [mapView setRegion:region animated:YES];
}

Define the multiple annotation here.

- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation
{

    if (annotation == mapView.userLocation)
        return nil;

    MKPinAnnotationView *pin = (MKPinAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier: @"asdf"];

    if (pin == nil)
        pin = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier: @"asdf"] autorelease];
    else
        pin.annotation = annotation;
        pin.userInteractionEnabled = YES;
        UIButton *disclosureButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        [disclosureButton setFrame:CGRectMake(0, 0, 30, 30)];

        pin.rightCalloutAccessoryView = disclosureButton;
        pin.pinColor = MKPinAnnotationColorRed;
        pin.animatesDrop = YES;
        [pin setEnabled:YES];
        [pin setCanShowCallout:YES];
        return pin;
}

More details and download the sample code here.

这篇关于添加多个注释iPhone MapView类是缓慢的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆