向 iPhone MapView 添加多个注释很慢

发布于 2024-12-23 18:59:16 字数 1186 浏览 1 评论 0原文

我的代码需要一个位置的标题和地址,然后从谷歌地图 csv 获取坐标,然后添加注释。它可以工作,但是当我想一次添加 50 个或更多注释时,它会冻结大约 30-40 秒,然后显示注释,所以我想知道的是我是否搞砸了内存管理,这就是它如此慢的原因,或者其他原因别的?我可以加快速度吗?

这是代码:

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

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

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

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

发布评论

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

评论(1

叶落知秋 2024-12-30 18:59:16

以下方法用于 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;
}

两个引脚之间的集中映射

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

在此处定义多个注释。

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

更多详情并下载示例代码在这里。

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.

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