2014-02-07 81 views
1

我已經添加了MKLocalSearch並且引腳顯示正確。唯一的問題是,針腳標題有姓名和地址,我只是想要這個名字。我將如何改變這一點。這裏是我使用的代碼 -從MKLocalSearch創建註釋的標題

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated 
{ 

    MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init]; 
    request.naturalLanguageQuery = @"School"; 
    request.region = mapView.region; 

    MKLocalSearch *localSearch = [[MKLocalSearch alloc] initWithRequest:request]; 
    [localSearch startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error) { 

     NSMutableArray *annotations = [NSMutableArray array]; 

     [response.mapItems enumerateObjectsUsingBlock:^(MKMapItem *item, NSUInteger idx, BOOL *stop) { 

      // if we already have an annotation for this MKMapItem, 
      // just return because you don't have to add it again 

      for (id<MKAnnotation>annotation in mapView.annotations) 
      { 
       if (annotation.coordinate.latitude == item.placemark.coordinate.latitude && 
        annotation.coordinate.longitude == item.placemark.coordinate.longitude) 
       { 
        return; 
       } 
      } 

      // otherwise, add it to our list of new annotations 
      // ideally, I'd suggest a custom annotation or MKPinAnnotation, but I want to keep this example simple 
      [annotations addObject:item.placemark]; 
     }]; 

     [mapView addAnnotations:annotations]; 
    }]; 
} 

回答

2

由於item.placemarktitle不能直接修改,你需要創建一個使用從item.placemark值的自定義批註或MKPointAnnotation

(在上面的addObject行代碼的註釋提到的「MKPinAnnotation」但我認爲這是爲了說「MKPointAnnotation」。)

下面的示例使用利用預先定義MKPointAnnotation的簡單的選擇該類由SDK提供,用於創建自己的簡單註釋。

替換此行:

[annotations addObject:item.placemark]; 

這些:

MKPlacemark *pm = item.placemark; 

MKPointAnnotation *ann = [[MKPointAnnotation alloc] init]; 
ann.coordinate = pm.coordinate; 
ann.title = pm.name; //or whatever you want 
//ann.subtitle = @"optional subtitle here"; 

[annotations addObject:ann];