使用MKLocalSearch在地图上搜索位置 [英] Use MKLocalSearch to search for locations on a map

查看:125
本文介绍了使用MKLocalSearch在地图上搜索位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用MKLocalSearch在地图中进行搜索.此功能在iOS 6.1+中可用.有人知道如何使用此方法吗?还是有人可以举一个如何使用MKLocalSearch的示例吗?

I want to use MKLocalSearch for searching in a Map. This functionality is available in iOS 6.1+. Does anybody know how to use this or can anybody give an example of how to use an MKLocalSearch?

推荐答案

用于

The API for MKLocalSearch is fairly easy to understand. At its most basic, you

  1. alloc-init MKLocalSearchRequest
  2. 将其naturalLanguageQuery设置为某个搜索词
  3. 使用搜索请求初始化MKLocalSearch对象
  4. 告诉本地搜索开始,并向其传递完成处理程序
  5. 对响应中的MKMapItem对象数组进行处理
  1. alloc-init an MKLocalSearchRequest
  2. Set its naturalLanguageQuery to some search term
  3. Use the search request to initialize an MKLocalSearch object
  4. Tell the local search to start, passing it a completion handler
  5. Do something with the array of MKMapItem objects in the response

搜索咖啡馆:

// 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]);
    }
}];


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


You can specify a region for the search like this:

// 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中获取区域.这样会产生更好的结果:

You can also take the region from an MKMapView that the user has zoomed into. This will give better results:

[searchRequest setRegion:self.mapView.region];


响应对象, MKLocalSearchResponse ,包含一个 MKMapItem 对象(mapItems )和称为boundingRegionMKCoordinateRegion,该区域包含所有结果.您可以使用它来设置地图视图以显示所有结果:


The response object, an MKLocalSearchResponse, contains an array of MKMapItem objects (mapItems) and an MKCoordinateRegion called boundingRegion, which is a region that contains all the results. You can use it to set a map view to show all results:

[self.mapView setRegion:response.boundingRegion];


MKMapItem对象的数组不能放置在地图上(它们用于发送到Maps应用),但是每个对象都包含一个placemark属性,可以将 添加到地图:


The array of MKMapItem objects can't be placed on a map (they're used for sending to the Maps app) but each contains a placemark property which can be added to a map:

[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]);
    }
}];

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

Search for Dublin places a pin on the map view and logs:

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


在返回的对象中有很多额外的细节,尤其是在搜索企业时.这里有一些:


There are a load of extra details in the returned objects, especially if you search for businesses. Here are a few:

[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);
    }

这篇关于使用MKLocalSearch在地图上搜索位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆