2010-02-26 128 views
20

我想在MKAnnotation中添加更多詳細信息,例如位置標題,說明,日期,位置名稱。所以這將是四條線所需要的。但是我發現只有2個參數可以傳遞給標題和副標題的MKAnnotation。我如何在地圖上添加更多細節? Plz幫助我..提前感謝。如何在iOS中的MKAnnotation中添加更多詳細信息

+3

我試圖通過添加到副標題的細節..但它顯示在一行..我如何能顯示多行細節 – 2010-02-26 13:59:53

回答

15

看看創建自定義MKAnnotationView對象......它基本上是一個爲地圖註釋量身定做的UIView。在那個對象中,你可以擁有4個自定義標籤。

在你MKMapViewDelegate類,需要實現viewForAnnotation方法:

- (CustomMapAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation 
{ 
    CustomMapAnnotationView *annotationView = nil; 

    // determine the type of annotation, and produce the correct type of annotation view for it. 
    CustomMapAnnotation* myAnnotation = (CustomMapAnnotation *)annotation; 

    NSString* identifier = @"CustomMapAnnotation"; 
    CustomMapAnnotationView *newAnnotationView = (CustomMapAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier]; 

    if(nil == newAnnotationView) { 
     newAnnotationView = [[[CustomMapAnnotationView alloc] initWithAnnotation:myAnnotation reuseIdentifier:identifier] autorelease]; 
    } 

    annotationView = newAnnotationView; 
    [annotationView setEnabled:YES]; 
    [annotationView setCanShowCallout:YES]; 

    return annotationView; 
} 

這將顯示在以往任何時候,如果你想一步步的教程,check out this video你有一個註釋...您的自定義視圖。

希望這有助於

編輯

我其實只是找到了一個新Maps example在蘋果開發站點...有你需要去通過所有的源代碼。他們也使用自定義MKAnnotationViewWeatherAnnotationView

+0

謝謝你的答案。我是否可以將細節添加到想要的格式的默認氣泡中,如使用換行符?我嘗試通過將UIlabel添加到MKPinAnnotationView的rightCalloutAccessoryView。但它不提供nice.thanks提前 – 2010-02-26 22:27:38

+0

我不會嘗試這樣做你試圖做到這一點...蘋果已設置註釋,以便他們可以擴展與MKAnnotationView類。試圖以你正在做的方式覆蓋正確的CalloutAccessoryView並不是一個好的方法來做到這一點......你正在與框架戰鬥,它給你帶來了奇怪的結果。如果您想要自定義註釋,最好的方式就是我描述的方式。 – 2010-02-27 16:15:18

+0

查看我的新編輯瞭解更多信息 – 2010-02-28 06:01:39

相關問題