我想使用 mapkit 框架获取附近位置的数组。因此,当用户在文本字段中键入时,我会调用以下函数。
- (void)searchForLocations:(NSString *)string
{
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(search:) object:nil];
[self performSelectorInBackground:@selector(search:) withObject:string];
}
- (void)search :(NSString *)string
{
MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init];
request.naturalLanguageQuery = string;
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta = 0.05;
span.longitudeDelta = 0.05;
region.span = span;
region.center = newLocation.coordinate;
request.region = region;
MKLocalSearch *search = [[MKLocalSearch alloc]initWithRequest:request];
[search startWithCompletionHandler:^(MKLocalSearchResponse
*response, NSError *error) {
if (response.mapItems.count == 0)
{
NSLog(@"No Matches");
}
else
{
NSLog(@"name = %@", item.name);
NSLog(@"Phone = %@", item.phoneNumber);
}
}];
}
如您所见,如果有新的输入文本出现,我想取消先前的搜索。但是之前的搜索并没有取消。如何取消之前的搜索?
提前致谢。