【发布时间】:2011-11-29 20:37:42
【问题描述】:
我正在使用以下代码通过地理编码获取位置信息,然后将地图图钉添加到 Google 地图视图。该代码使用 For 循环循环遍历我数据库中的每个位置。问题是代码在运行时无法返回大约 50% 的位置信息。这些失败的项目按照下面的代码保存在 failedLoad 数组中。
谁能说明这是为什么?此外,由于这些失败的项目保存在“failedLoad”数组中,有没有办法可以使用这个数组来加载任何丢失的引脚?
编辑
失败的项目是由于 620 错误,这意味着我提交项目太快了。如何在代码中添加延迟?
谢谢!
-(void)displayPlaces {
for (PlaceObject *info in mapLocations) {
// 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];
NSString* urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv", [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSURL* url = [NSURL URLWithString:urlString];
NSURLRequest* req = [NSURLRequest requestWithURL:url];
OHURLLoader* loader = [OHURLLoader URLLoaderWithRequest:req];
[loader startRequestWithCompletion:^(NSData* receivedData, NSInteger httpStatusCode) {
NSString* locationString = loader.receivedString;
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];
[mapViewLink addAnnotation:annotation];
} errorHandler:^(NSError *error) {
NSLog(@"Error while downloading %@: %@",url,error);
}];
}
}
【问题讨论】:
-
Google API 有各种查询速率限制,例如每秒最多 10 个请求。如果您打算使用 API,那么您真的应该对所有这些进行 RTFM。
-
当位置加载失败时,为什么不记录返回的字符串。失败的原因将立即显而易见。
-
谢谢 - 这是一个 620 错误,我知道这是由于我提交的速率所致。我将如何延迟代码?
-
请注意,这个问题是the followup of this one(但这不是重复的,因为目前的问题是关于 Google API 速率限制和添加请求之间的延迟 - 刚刚提到对于那些对我们如何获得该代码感兴趣的人来说,之前的这篇文章)
-
如果我记得很清楚,Google Maps Web 服务允许在一次调用中聚合多个请求(我可能是错的)。看看code.google.com/apis/maps/documentation/webservices/index.html
标签: iphone objective-c xcode mkmapview geocoding