2011-05-01 73 views
0

我在我的应用程序下面的代码,通过谷歌的地理编码增加了一些注释,以一个MapView:加速谷歌地理编码的addAnnotations?

for (PlaceObject *info in mapLocations) { 

    NSString * addressOne = info.addressOne; 
    NSString * name = info.name; 

    NSString * address = [addressOne stringByAppendingString:@",London"]; 

    NSError * error; 

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

    NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString ] encoding:NSASCIIStringEncoding error:&error]; 
    NSArray *listItems = [locationString componentsSeparatedByString:@","]; 

    double latitude = 0.0; 
    double longitude = 0.0; 

    if([listItems count] >= 4 && [[listItems objectAtIndex:0] isEqualToString:@"200"]) { 
     latitude = [[listItems objectAtIndex:2] doubleValue]; 
     longitude = [[listItems objectAtIndex:3] doubleValue]; 

    } else { 
     //Error handling 

    }   

    CLLocationCoordinate2D coordinate; 
    coordinate.latitude = latitude; 
    coordinate.longitude = longitude; 
    MyLocation *annotation = [[[MyLocation alloc] initWithName:name address:address coordinate:coordinate] autorelease]; 
    [mapViewLink addAnnotation:annotation]; 

} 

这工作,但它需要很长的时间,以便出现,因为它似乎要等到数组中的所有对象都已循环(大约有80个)。

有没有什么办法让视图加载,然后在创建引脚时添加引脚?

回答

1

如何:

for (PlaceObject *info in mapLocations) { 

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); 
    dispatch_async(queue, ^{ 

     // GET ANNOTATION INFOS 
     NSString * addressOne = info.addressOne; 
     NSString * name = info.name; 

     NSString * address = [addressOne stringByAppendingString:@",London"]; 

     NSError * error; 

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

     NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString ] encoding:NSASCIIStringEncoding error:&error]; 
     NSArray *listItems = [locationString componentsSeparatedByString:@","]; 

     double latitude = 0.0; 
     double longitude = 0.0; 

     if([listItems count] >= 4 && [[listItems objectAtIndex:0] isEqualToString:@"200"]) { 
      latitude = [[listItems objectAtIndex:2] doubleValue]; 
      longitude = [[listItems objectAtIndex:3] doubleValue]; 

     } else { 
      //Error handling 

     }   

     CLLocationCoordinate2D coordinate; 
     coordinate.latitude = latitude; 
     coordinate.longitude = longitude; 
     MyLocation *annotation = [[[MyLocation alloc] initWithName:name address:address coordinate:coordinate] autorelease]; 

     dispatch_sync(dispatch_get_main_queue(), ^{ 

      // ADD ANNOTATION 
      [mapViewLink addAnnotation:annotation]; 

     }); 

    }); 
} 

但是请注意仔细检查这些代码。对于保持线程安全并避免EXC_BAD_ACCESS,你应该做的事情是确定的。

0

是的,你正在做主要(用户界面)线程做长时间工作(网络加载)的经典错误。将这个工作移动到后台线程中,然后让主线程添加管脚。这可以通过几种方式创建另一个显式线程,带块的GCD或仅在后台线程中执行选择器。最简单的方法是在后台执行选择器,然后当它有一些东西添加到地图上时,在主线程上执行选择器。