【发布时间】:2011-11-29 12:26:14
【问题描述】:
我的应用中有一个谷歌地图视图,该视图通过地理编码填充了引脚。我正在使用下面的代码创建一个调度队列,然后向 Google 查询我的应用中每个地方的经度和纬度。
问题在于,尽管下面的代码在一定程度上有效,但它似乎在第一次运行时遗漏了很大比例的项目。根据下面的代码,这些项目被添加到数组“failedLoad”中。
目前,我正在运行第二种方法来在 failedLoad 中添加位置,每当调用 ViewDidLoad 方法时都会调用该方法。然而,这是一个糟糕的解决方案,因为即使在第二种方法运行之后,failedLoad 中仍然有项目,而且我更希望在不依赖 ViewDidLoad 的情况下加载所有引脚(仅在用户点击引脚时调用,然后返回从显示的详细视图屏幕)。
谁能提出一个改进这个过程的好方法?
谢谢
-(void)displayPlaces {
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 * postCode = info.postCode;
NSString * addressTwo = [addressOne stringByAppendingString:@",London,"];
NSString * address = [addressTwo stringByAppendingString:postCode];
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 {
NSLog(@"Error %@",name);
[failedLoad addObject : info];
}
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];
});
});
}
【问题讨论】:
标签: iphone objective-c multithreading google-maps geocoding