我在我的应用程序中有以下代码,它通过Google地理编码向地图视图添加了一些注释:
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个)。
有没有办法加载视图,然后在创建管脚时添加管脚?
发布于 2011-05-02 04:05:03
这样如何:
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
,你应该做一些明确的事情。
发布于 2011-05-02 04:02:38
这段代码在你的应用程序中的什么地方执行?
我建议使用NSURLConnection
,并在接收和解析数据之后添加注释。如果你不熟悉NSURLConnection,可以在这里查看更多信息:http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html
发布于 2011-05-02 04:03:03
是的,你犯了一个典型的错误,那就是在主(UI)线程中做长时间的工作(从URL净加载)。将这项工作移到后台线程中,然后让主线程添加引脚。这可以通过几种方式,通过创建另一个显式线程,使用块的GCD,或者只是在后台线程中的performSelector。最简单的方法可能是在后台performSelector,然后当它有东西要添加到地图时,在主线程上执行performSelector。
https://stackoverflow.com/questions/5850930
复制相似问题