Ale*_*one 7 iphone multithreading objective-c ios5 clgeocoder
我得到了大约200个地址,我需要知道它们的纬度和经度.我已经创建了一个解析地址的方法,现在我正在尝试使用这些地址获取坐标CLGeocoder.
我目前的方法是并行创建地理编码器,让他们发挥他们的魔力.我注意到他们每个人似乎都采取了一个单独的线程.(所以我在一个点上看到多达100个线程).
我遇到的问题是,在某些时候(大约50个地址之后),地理编码停止返回任何地方标记,并且
NSLog(@"Address not recognized: *%@*",[htc objectForKey:kAddressKey]);
Run Code Online (Sandbox Code Playgroud)
被叫.这是对多个线程或内置CLGeocoder限制的限制吗?可能是因为我没有正确清理地理编码器并需要某种自动释放声明(ARC)吗?
- (void)geocodeArray:(NSMutableArray*)array {
NSMutableDictionary* htc = nil;
objectsToGeocode = array.count;
NSDictionary *htcDictionary =nil;
for (int i = 0; i<array.count;i++) {
htcDictionary = [array objectAtIndex:i];
//create an updated dictionary that would hold the reverse geocoding location
htc = [[NSMutableDictionary alloc] initWithDictionary:htcDictionary];
NSLog(@"geocoding: %@",[htc objectForKey:kAddressKey]);
CLGeocoder* geoCoder = [[CLGeocoder alloc] init];
[geoCoder geocodeAddressString:[htc objectForKey:kAddressKey] completionHandler:^(NSArray *placemarks, NSError *error) {
if(placemarks.count>0)
{
NSLog(@"Found placemarks for %@",[htc objectForKey:kAddressKey]);
CLPlacemark* placemark = [placemarks objectAtIndex:0];
MyLocation *annotation = [[MyLocation alloc]
initWithName:[htcDictionary objectForKey:kNameKey]
address:[htcDictionary objectForKey:kAddressKey]
coordinate:placemark.location.coordinate] ;
annotation.faxNumber = [htc objectForKey:kFaxKey];
annotation.phoneNumber = [htc objectForKey:kPhoneKey];
annotation.website = [htc objectForKey:kWebsiteKey];
annotation.type = [htc objectForKey:kFacilityTypeKey];
[_mapView addAnnotation:annotation];
double placemarkToUserDistance = [self._mapView.userLocation.location distanceFromLocation:placemark.location] ;
//convert distance to miles
placemarkToUserDistance =placemarkToUserDistance/ 1000/ kKilometersPerMile;
[htc setObject:[NSNumber numberWithDouble:placemarkToUserDistance] forKey:kDistanceToUserKey];
[htc setObject:[NSNumber numberWithDouble:placemark.location.coordinate.latitude] forKey:kLatitudeKey];
[htc setObject:[NSNumber numberWithDouble:placemark.location.coordinate.longitude] forKey:kLongitudeKey];
NSAssert([htc objectForKey:kLatitudeKey]!=nil,@"kLatitudeKey is not saved!");
NSAssert([htc objectForKey:kLongitudeKey]!=nil,@"kLongitudeKey is not saved!");
}else {
NSLog(@"Address not recognized: *%@*",[htc objectForKey:kAddressKey]);
}
[self.dataSource addObject:htc];
if(++geocodingCount >=objectsToGeocode){
NSLog(@"%@",self.dataSource);
[self saveGeocoding];
}
} ];
// [temp addObject:htcDictionary];
}
}
Run Code Online (Sandbox Code Playgroud)
为了测试这是否是一个线程问题,我创建了这个方法,它将我的大数据集拆分为5个数组,并尝试将它们分块进行地理编码.我注意到第一个请求通过,第二个请求通过.但是一旦达到(~50)的幻数,地理编码就会停止.
对可能发生的事情的任何想法?这是Apple对地理编码操作数量的限制吗?我应该增加请求之间的延迟还是尝试分别运行应用程序5并手动拼凑结果?
-(void)geocodeDatasource
{
//I'm trying to build a file with coordinates of addresses and include it with the app
geocodingCount = 0;
self.dataSource = [[NSMutableArray alloc] initWithCapacity:self.arrayForGeocodingInitialJSON.count+5];
haveToEmailInitialResults = YES;
//attempt to geocode in batches
float numberOfArrays = 5.0;
NSMutableArray* array1 = [[NSMutableArray alloc] initWithCapacity:arrayForGeocodingInitialJSON.count/numberOfArrays];
NSMutableArray* array2 = [[NSMutableArray alloc] initWithCapacity:arrayForGeocodingInitialJSON.count/numberOfArrays];
NSMutableArray* array3 = [[NSMutableArray alloc] initWithCapacity:arrayForGeocodingInitialJSON.count/numberOfArrays];
NSMutableArray* array4 = [[NSMutableArray alloc] initWithCapacity:arrayForGeocodingInitialJSON.count/numberOfArrays];
NSMutableArray* array5 = [[NSMutableArray alloc] initWithCapacity:arrayForGeocodingInitialJSON.count/numberOfArrays];
for(int i = 0 ;i<arrayForGeocodingInitialJSON.count;i++)
{
id object = [arrayForGeocodingInitialJSON objectAtIndex:i];
if(i<arrayForGeocodingInitialJSON.count*(1/numberOfArrays))
{
[array1 addObject:object];
}else if(i>=arrayForGeocodingInitialJSON.count/numberOfArrays && i<arrayForGeocodingInitialJSON.count*(2/numberOfArrays))
{
[array2 addObject:object];
}else if(i>=arrayForGeocodingInitialJSON.count*(2/numberOfArrays) && i<arrayForGeocodingInitialJSON.count*(3/numberOfArrays))
{
[array3 addObject:object];
}else if(i>=arrayForGeocodingInitialJSON.count*(3/numberOfArrays) && i<arrayForGeocodingInitialJSON.count*(4/numberOfArrays))
{
[array4 addObject:object];
}else if(i>=arrayForGeocodingInitialJSON.count*(4/numberOfArrays) && i<arrayForGeocodingInitialJSON.count)
{
[array5 addObject:object];
}
}
//simple delays eliminate the need for extra variables and notifications
[self geocodeArray:array2];
[self performSelector:@selector(geocodeArray:) withObject:array1 afterDelay:15];
[self performSelector:@selector(geocodeArray:) withObject:array3 afterDelay:30];
[self performSelector:@selector(geocodeArray:) withObject:array4 afterDelay:45];
[self performSelector:@selector(geocodeArray:) withObject:array5 afterDelay:45];
}
Run Code Online (Sandbox Code Playgroud)
谢谢!
NSP*_*mer 17
您无法立即对大型地理位置进行地理编码.iOS限制了你.我已经看到iOS一次限制你50个地理编码,"时间"因素是未知的.
我遇到了类似的问题,但由于我只需要将序列中的地理编码数据呈现给需要时间的用户,我将所有的地理编码排队.
实际上,我对25块进行了地理编码 - 每次以大约半秒的间隔向用户1显示结果.当我剩下少于4个显示时,我将对下一个25进行地理编码.这将一直持续到所有地理编码(或者在我的情况下,无限期).
如果您需要同时对所有地理编码进行地理编码,则需要将每个块之间的延迟链接在一起,并显示某种忙碌指示符,直到完成为止.大型地理编码可能需要一段时间.
答案是你违反了苹果地理编码器的服务条款.
从CLGeocoder文档.
应用程序应该意识到它们如何使用地理编码.以下是有效使用此类的一些经验法则:
为任何一个用户操作最多发送一个地理编码请求.如果用户执行涉及对同一位置进行地理编码的多个操作,请重复使用初始地理编码请求的结果,而不是为每个操作启动单个请求.当您想要自动更新用户的当前位置时(例如当用户移动时),仅在用户移动了相当长的距离并经过一段合理的时间后才发出新的地理编码请求.例如,在典型情况下,您不应每分钟发送多个地理编码请求.当用户不立即看到结果时,请勿启动地理编码请求.例如,如果您的应用程序处于非活动状态或在后台运行,请不要启动请求.
Apple完全有权停止提供回复,完全切断您的应用,甚至撤销您的开发者帐户.
如果您需要快速处理如此大量的地址,那么您应该考虑获得像Factual for Interest of Interest或Data Science Toolkit这样的服务,用于邮政地址和地理区域.
更新
以下是Places API - Resolve的API文档,它是Factual的地方编码器.
更新2
以下是街道地址到坐标的API ,它是Data Science Toolkit的位置地理编码器.
| 归档时间: |
|
| 查看次数: |
7397 次 |
| 最近记录: |