使用 MKLocalSearch 进行本地搜索实现

MKLocalSearch 允许用户使用健身房等自然语言字符串搜索位置。搜索完成后,该类将返回指定区域内与搜索字符串匹配的位置列表。

搜索结果是 MKLocalSearchResponse 对象中的 MKMapItem 形式。

让我们尝试一下

MKLocalSearchRequest *request = 
    [[MKLocalSearchRequest alloc] init];//initialising search request
request.naturalLanguageQuery = @”Gym”; // adding query
request.region = _mapView.region; //setting region
MKLocalSearch *search = 
     [[MKLocalSearch alloc]initWithRequest:request];//initiate search

[search startWithCompletionHandler:^(MKLocalSearchResponse 
     *response, NSError *error) 
{
   if (response.mapItems.count == 0)
       NSLog(@"No Matches");
   else
       for (MKMapItem *item in response.mapItems)
       {
           NSLog(@"name = %@", item.name);
           NSLog(@"Phone = %@", item.phoneNumber);
        }
}];