ios – 使用MKLocalSearch搜索地图上的位置

前端之家收集整理的这篇文章主要介绍了ios – 使用MKLocalSearch搜索地图上的位置前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想使用MKLocalSearch在地图中搜索.此功能在iOS 6.1中可用.有人知道如何使用这个或任何人都可以举例说明如何使用MKLocalSearch?

MKLocalSearchResponse documentation

解决方法

MKLocalSearch的API非常容易理解.最基本的,你

> alloc-init和MKLocalSearchRequest
>将其naturalLanguageQuery设置为某个搜索
>使用搜索请求初始化MKLocalSearch对象
>告诉本地搜索开始,将其传递给完成处理程序
>对响应中的MKMapItem对象数组执行某些操作

搜索咖啡馆:

  1. // Create a search request with a string
  2. MKLocalSearchRequest *searchRequest = [[MKLocalSearchRequest alloc] init];
  3. [searchRequest setNaturalLanguageQuery:@"Cafe"];
  4.  
  5. // Create the local search to perform the search
  6. MKLocalSearch *localSearch = [[MKLocalSearch alloc] initWithRequest:searchRequest];
  7. [localSearch startWithCompletionHandler:^(MKLocalSearchResponse *response,NSError *error) {
  8. if (!error) {
  9. for (MKMapItem *mapItem in [response mapItems]) {
  10. NSLog(@"Name: %@,Placemark title: %@",[mapItem name],[[mapItem placemark] title]);
  11. }
  12. } else {
  13. NSLog(@"Search Request Error: %@",[error localizedDescription]);
  14. }
  15. }];

您可以为此搜索指定区域:

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

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

  1. [searchRequest setRegion:self.mapView.region];

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

  1. [self.mapView setRegion:response.boundingRegion];

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

  1. [localSearch startWithCompletionHandler:^(MKLocalSearchResponse *response,MKAnnotation title: %@",[[mapItem placemark] title]);
  2. NSLog(@"Coordinate: %f %f",[[mapItem placemark] coordinate].latitude,[[mapItem placemark] coordinate].longitude);
  3. // Should use a weak copy of self
  4. [self.mapView addAnnotation:[mapItem placemark]];
  5. }
  6. } else {
  7. NSLog(@"Search Request Error: %@",[error localizedDescription]);
  8. }
  9. }];

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

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

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

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

猜你在找的iOS相关文章