使用MKLocalSearch搜索地图上的位置

时间:2012-12-10 10:11:12

标签: ios objective-c mkmapview mapkit mklocalsearch

我想使用MKLocalSearch在地图中搜索。此功能在iOS 6.1+中可用。有人知道如何使用这个或任何人都可以举例说明如何使用MKLocalSearch

MKLocalSearchResponse documentation

3 个答案:

答案 0 :(得分:23)

MKLocalSearch的API相当容易理解。你最基本的是

  1. alloc-initMKLocalSearchRequest
  2. 将其naturalLanguageQuery设置为某个搜索字词
  3. 使用搜索请求初始化MKLocalSearch对象
  4. 告诉本地搜索开始,将其传递给完成处理程序
  5. 对响应中的MKMapItem个对象执行某些操作
  6. 搜索咖啡馆:

    // Create a search request with a string 
    MKLocalSearchRequest *searchRequest = [[MKLocalSearchRequest alloc] init];
    [searchRequest setNaturalLanguageQuery:@"Cafe"];
    
    // Create the local search to perform the search
    MKLocalSearch *localSearch = [[MKLocalSearch alloc] initWithRequest:searchRequest];
    [localSearch startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error) {
        if (!error) {
            for (MKMapItem *mapItem in [response mapItems]) {
                NSLog(@"Name: %@, Placemark title: %@", [mapItem name], [[mapItem placemark] title]);
            }
        } else {
            NSLog(@"Search Request Error: %@", [error localizedDescription]);
        }
    }];
    

    您可以像这样指定搜索区域:

    // Search for Cafes in Paris 
    MKLocalSearchRequest *searchRequest = [[MKLocalSearchRequest alloc] init];
    [searchRequest setNaturalLanguageQuery:@"Cafe"];
    CLLocationCoordinate2D parisCenter = CLLocationCoordinate2DMake(48.8566667, 2.3509871);
    MKCoordinateRegion parisRegion = MKCoordinateRegionMakeWithDistance(parisCenter, 15000, 15000);
    [searchRequest setRegion:parisRegion];
    

    您还可以从用户放大的MKMapView中获取区域。这将带来更好的结果:

    [searchRequest setRegion:self.mapView.region];
    

    响应对象MKLocalSearchResponse包含一组MKMapItem个对象(mapItems)和一个名为MKCoordinateRegion的{​​{1}},该区域是包含所有结果。您可以使用它来设置地图视图以显示所有结果:

    boundingRegion

    [self.mapView setRegion:response.boundingRegion]; 个对象的数组不能放在地图上(它们用于发送到地图应用)但每个都包含一个{em}可以MKMapItem属性>被添加到地图中:

    placemark

    搜索都柏林在地图视图和日志上放置一个图钉:

    [localSearch startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error) {
        if (!error) {
            for (MKMapItem *mapItem in [response mapItems]) {
                NSLog(@"Name: %@, MKAnnotation title: %@", [mapItem name], [[mapItem placemark] title]);
                NSLog(@"Coordinate: %f %f", [[mapItem placemark] coordinate].latitude, [[mapItem placemark] coordinate].longitude);
                // Should use a weak copy of self
                [self.mapView addAnnotation:[mapItem placemark]];
            }
        } else {
            NSLog(@"Search Request Error: %@", [error localizedDescription]);
        }
    }];
    

    返回的对象中有大量额外的细节,特别是在您搜索商家时。以下是一些:

    Name: Dublin, Co. Dublin, MKAnnotation title: Dublin, Co. Dublin, Ireland
    Coordinate: 53.344104 -6.267494
    

答案 1 :(得分:6)

以下是在给定位置周围1公里范围内搜索咖啡馆的示例:

MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init];
CLLocationCoordinate2D location = CLLocationCoordinate2DMake(11.567898, 104.894430);
request.naturalLanguageQuery = @"cafe";
request.region = MKCoordinateRegionMakeWithDistance(location, 1000, 1000);
MKLocalSearch *search = [[MKLocalSearch alloc] initWithRequest:request];
[search startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error){
    for (MKMapItem *item in response.mapItems) {
        NSLog(@"%@", item.name);
    }
}];

请注意,与搜索失败相比,它不会返回空列表,但会显示域MKErrorDomain和代码4的错误。

答案 2 :(得分:1)