ios - 批量地理编码 MKLocalSearch 响应
<p><p>我有一个搜索设置,它在 UITableVIew 中显示 MKLocalSearchRequest 的响应</p>
<p>我想从每次搜索的响应中清理<em>所有</em>响应。到目前为止,这是我在这篇文章 <a href="https://stackoverflow.com/questions/14195706/multiple-locations-on-map-using-mkmapitem-and-clgeocoder/14198584?noredirect=1#comment39688814_14198584" rel="noreferrer noopener nofollow">Multiple Locations on Map (using MKMapItem and CLGeocoder)</a> 的启发下完成这项工作的机会。 </p>
<p>这是我的代码。 </p>
<pre><code>@interface ViewController () <UISearchBarDelegate,UISearchDisplayDelegate,UITextFieldDelegate>
@property (nonatomic, strong) UISearchDisplayController *searchController;
@property (nonatomic, strong) UISearchBar *searchBar;
@property (nonatomic, strong) MKLocalSearch *localSearch;
@property (nonatomic, strong) MKLocalSearchResponse *localSearchResponse;
@property (nonatomic, strong) NSArray *dirtyResponseArray;
@property (nonatomic, strong) NSMutableArray *geocodedResultsArray;
@end
@implementation ViewController
-(void)startSearch:(NSString *)searchString
{
if (self.localSearch.searching)
;
MKLocalSearchRequest *request = [ init];
request.naturalLanguageQuery = searchString;
request.region = MKCoordinateRegionMake(self.currentLocation.coordinate, self.mapView.region.span);
MKLocalSearchCompletionHandler completionHandler = ^(MKLocalSearchResponse *response, NSError *error){
if (error != nil) return;
else {
self.dirtyResponseArray = response.mapItems;
;
;
.networkActivityIndicatorVisible = NO;
}
;
};
if (self.localSearch != nil)
self.localSearch = nil;
self.localSearch = [ initWithRequest:request];
;
}
-(void)operation
{
CLGeocoder *geocoder = [init];
NSOperationQueue *queue = [ init];
NSOperation *finalCompletionOperation = [NSBlockOperation blockOperationWithBlock:^{
;
NSLog(@"Local Search Response To use in tableview =================== %@", self.geocodedResultsArray);
}];
NSOperation *previousCompletionHandler = nil;
//Issue is probably right here. How should I handle the MKMapItem in the array?
//NSString *address = [@"formattedAddress"]; ??
for (NSString *address in self.dirtyResponseArray) {
NSBlockOperation *geocodeRequest = [ init];
if (previousCompletionHandler) ;
NSBlockOperation *geocodeCompletionHandler = [ init];
;
[geocodeRequest addExecutionBlock:^{ [geocoder geocodeAddressString:address
completionHandler:^(NSArray *placemarks, NSError *error)
{
[geocodeCompletionHandler addExecutionBlock:^{
if (error) NSLog(@"%@", error);
else if ( > 0)
{
CLPlacemark *geocodedPlacemark = ;
MKPlacemark *placemark = [ initWithCoordinate:geocodedPlacemark.location.coordinate
addressDictionary:geocodedPlacemark.addressDictionary];
MKMapItem *mapItem = [ initWithPlacemark:placemark];
;
;
}
}];
;
}];
}];
;
previousCompletionHandler = geocodeCompletionHandler;
}
;
}
@end
</code></pre>
<p>我不确定如何处理每个 MKMapItem。现在它抛出这个错误</p>
<pre><code> -: unrecognized selector sent to instance 0x7f813c9c6200
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>在另一个问题中引用的 <code>CLGeocoder</code> 方法 <code>geocodeAddressString</code> 旨在对地址进行地理编码。</p>
<p>但是您正在特定区域内执行 <code>MKLocalSearch</code>,它返回已被地理编码的 <code>MKMapItem</code> 对象。因此,您可以从代码示例中完全删除 <code>geocodeAddressString</code> 代码。 </p>
<p>事实上,这解释了您的异常的来源,即您正在获取 <code>MKLocalSearch</code> 返回的 <code>MKMapItem</code>,并尝试将其用作搜索字符串参数<code>CLGeocoder</code> 方法 <code>geocodeAddressString</code>(仅用于查找地址的字符串表示形式)。</p>
<p>最重要的是,进行本地搜索并在 map 应用程序中显示结果比您在上面所设想的要容易得多。您需要做的就是:</p>
<pre><code>MKLocalSearchRequest *request = [ init];
request.naturalLanguageQuery = searchString;
request.region = region;
MKLocalSearch *search = [ initWithRequest:request];
[search startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error) {
;
}];
</code></pre>
<p>或者,如果你想在自己的 <code>MKMapView</code> 上显示它,你可以这样做</p>
<pre><code>MKLocalSearchRequest *request = [ init];
request.naturalLanguageQuery = searchString;
request.region = region;
MKLocalSearch *search = [ initWithRequest:request];
[search startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error) {
for (MKMapItem *item in response.mapItems) {
;
}
}];
</code></pre>
<p>实际上,在后一个示例中,您在自己的 mapView 上显示结果,您可以创建自己的自定义注释类,以便更好地控制 <code>viewForAnnotation</中注释 View 的呈现</code>方法。但希望这能说明主要观察结果,即在使用 <code>MKLocalSearch</code> 时,您根本不应该使用 <code>CLGeocoder</code> 方法 <code>geocodeAddressString</code>。</p></p>
<p style="font-size: 20px;">关于ios - 批量地理编码 MKLocalSearch 响应,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/25438928/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/25438928/
</a>
</p>
页:
[1]