2012-01-03 43 views
1

我有代碼需要標題和地址的位置,然後從谷歌地圖CSV獲得座標,然後添加註釋。它可以工作,但是當我想要一次添加50個或更多註釋時,它會凍結大約30-40秒,然後顯示註釋,所以我想知道的是我搞砸了內存管理,這就是爲什麼它很慢或者什麼其他?我可以加快速度嗎?添加多個批註到iPhone MapView很慢

下面是代碼:

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

現在崩潰時,我試圖打開的應用程序和負載50個註解 – 1337code 2012-01-03 10:51:51

+0

的'stringWithContentsOfURL'方法是同步的,直到其完成將阻止用戶界面。如果你在一個循環內調用這個方法,UI將會被阻塞很長時間。這裏只是一些替代方案:http://stackoverflow.com/questions/5850930/speed-up-addannotations-from-google-geocoding和http://stackoverflow.com/questions/7634246/for-loop-with- google-geocoding-high-error-rate-iphone – Anna 2012-01-03 14:45:00

回答

1

下面的方法使用了在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; 
} 

More details and download the sample code here.

+0

不錯的代碼,很容易實現。乾杯! – Felipe 2014-07-21 14:25:14