2012-12-10 179 views

回答

21

MKLocalSearch的API很容易理解。在最基本的,你

  1. alloc-initMKLocalSearchRequest
  2. 設置其naturalLanguageQuery一些搜索詞
  3. 使用搜索請求初始化一個MKLocalSearch對象
  4. 告訴本地搜索開始,傳遞一個完成處理程序
  5. 在響應中使用MKMapItem對象的數組做什麼

搜索咖啡館:

// 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用戶有:

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

您可以這樣搜索指定區域放大。這將給出更好的結果:

[searchRequest setRegion:self.mapView.region]; 

響應對象中,MKLocalSearchResponse,包含MKMapItem對象(mapItems)和MKCoordinateRegion稱爲boundingRegion,它是一個包含所有的結果的區域的陣列。你可以用它來設置地圖視圖顯示所有結果:

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

相關問題