使用MKLocalSearch在地图上搜索位置

9
我想使用MKLocalSearch在地图中进行搜索。这个功能在iOS 6.1+中可用。有人知道如何使用它,或者可以给出一个MKLocalSearch的使用示例吗? MKLocalSearchResponse文档
3个回答

24

MKLocalSearch的API相当容易理解。其最基本的用法是:

  1. alloc-init一个MKLocalSearchRequest对象
  2. 将它的naturalLanguageQuery设置为某个搜索词
  3. 使用该搜索请求来初始化一个MKLocalSearch对象
  4. 告诉本地搜索开始工作,并传递一个完成处理程序
  5. 使用响应中的MKMapItem对象数组执行某些操作

搜索咖啡馆:

// 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) 和一个名为 boundingRegionMKCoordinateRegion,它是包含所有结果的区域。您可以使用它来设置地图视图以显示所有结果:
[self.mapView setRegion:response.boundingRegion];

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

返回的对象中有很多额外的细节,特别是如果你搜索企业。以下是一些示例:

[localSearch startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error) {
    if (!error) {
        NSLog(@"Results: %@", [response mapItems]);
        MKMapItem *mapItem = [[response mapItems] objectAtIndex:0];
        NSLog(@"Name:%@ Phone:%@ URL:%@", [mapItem name], [mapItem phoneNumber], [mapItem url]);
        NSLog(@"Placemark: %@", [mapItem placemark]);
        MKPlacemark *placemark = [mapItem placemark];
        NSLog(@"Placemark Address: %@", [placemark addressDictionary]);
        MKCoordinateRegion boundingRegion = [response boundingRegion];
        NSLog(@"Bounds: %f %f", boundingRegion.span.latitudeDelta, boundingRegion.span.longitudeDelta);
    }

7
这是一个搜索给定位置周围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的错误。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接