【发布时间】:2012-01-03 10:48:19
【问题描述】:
我的代码获取位置的标题和地址,然后从谷歌地图 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];
}
【问题讨论】:
-
当我尝试打开应用程序并加载 50 个注释时,它现在崩溃了
-
stringWithContentsOfURL方法是同步的,会阻塞 UI 直到完成。如果在循环中调用此方法,UI 将被阻塞很长时间。这里只是几个选择:stackoverflow.com/questions/5850930/… 和 stackoverflow.com/questions/7634246/…
标签: iphone annotations android-mapview