发出前向地理编码多个地址
Posted
技术标签:
【中文标题】发出前向地理编码多个地址【英文标题】:Issue forward-geocoding multiple addresses 【发布时间】:2011-11-12 21:37:25 【问题描述】:我正在连接到一个基本上返回 XML 的远程 Web 服务。然后我将该 XML 解析为一个 Property 对象(想想真实状态的东西)
但是现在,Web 服务仅返回每个属性的邮政编码。它不提供我在地图中放置注释所需的坐标。我能够对提供邮政编码的地址进行地理编码。但是,我的问题是它不允许我执行 多个请求
这是我的代码
- (void)processProperties:(Property *)property
[geocoder geocodeAddressString:property.postalCode
completionHandler:^(NSArray* placemarks, NSError* error)
placemark = [placemarks lastObject];
for (CLPlacemark* aPlacemark in placemarks)
[sublet setLatitude:aPlacemark.location.coordinate.latitude];
[sublet setLongitude:aPlacemark.location.coordinate.longitude];
];
- (void)addAnnotations:(NSArray *)objects
CLLocationDegrees lat;
CLLocationDegrees longitude;
CLLocationCoordinate2D mCoords;
NSString *fullAddress;
// Add the annotations found nearby
for (Property *property in objects)
[self processProperties:property];
lat = property.latitude;
longitude = property.longitude;
fullAddress = [NSString stringWithFormat:@"%@ %@ %@", property.houseNumber, @" ", property.streetName];
[self createAnnotationWithCoords:mCoords :fullAddress :[NSString stringWithFormat:@"$%.2f", property.rent]];
zoomLevel = 0.1;
mCoords = CLLocationCoordinate2DMake(lat,longitude);
MKCoordinateRegion region = MKCoordinateRegionMake(mCoords,MKCoordinateSpanMake(zoomLevel,zoomLevel));
[self.mapView setRegion:region animated:YES];
出于某种原因,它只是对 1 个属性进行地理编码。没有相应地通过循环。
各位有什么想法吗?
【问题讨论】:
【参考方案1】:在您的前向地理功能上使用它。需要重新发布地理编码器并重新初始化以启动新地址,希望对您有所帮助。
- (void)processProperties:(Property *)property
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:property.postalCode
completionHandler:^(NSArray* placemarks, NSError* error)
placemark = [placemarks lastObject];
for (CLPlacemark* aPlacemark in placemarks)
[sublet setLatitude:aPlacemark.location.coordinate.latitude];
[sublet setLongitude:aPlacemark.location.coordinate.longitude];
[geocoder release];
];
【讨论】:
Apple 似乎建议每个应用会话只进行一次地理编码,这是非常有限的。你是对的,他们似乎通过只允许一个实例调用来强制执行这一点。创建多个实例可以解决问题,但这确实是一件奇怪的事情。以上是关于发出前向地理编码多个地址的主要内容,如果未能解决你的问题,请参考以下文章