2011-02-10 202 views
1

使用下面的代碼,我可以顯示標題和副標題,並且我想在其中顯示圖像。那可能嗎?如果是這樣,請幫助我。在地圖註釋中顯示圖像

MKPointAnnotation *aAnnotationPoint = [[MKPointAnnotation alloc] init]; 

aAnnotationPoint.title = @"Virginia"; 

aAnnotationPoint.subtitle = @"Test of sub title"; 

// Add the annotationPoint to the map 
[myMapView addAnnotation:aAnnotationPoint]; 
+0

問題有 「iphone-SDK-3.0」 的標籤,但在4.0中添加了MKPointAnnotation。你真的意思是3.0嗎? – Anna 2011-02-10 04:08:28

+0

對不起,這是一個錯字 – user198725878 2011-02-10 04:15:59

回答

17

我假設你是顯示在標註爲註釋的標題和副標題(當你點擊的引腳上,如果您使用的是引腳出現的灰色框)。如果是這樣,該標註有兩個視圖供您自定義(leftCalloutAccessoryView和rightCalloutAccessoryView(均在註釋視圖中設置))

如果您希望圖像出現在銷釘上方的灰色框中腳,你可以通過自定義標註視圖這樣做通過實施委託方法是這樣的:

-(MKAnnotationView*)mapView:(MKMapView*)mapView viewForAnnotation:(id<MKAnnotation>)annotation { 
    // If you are showing the users location on the map you don't want to change it 
    MKAnnotationView *view = nil; 
    if (annotation != mapView.userLocation) { 
    // This is not the users location indicator (the blue dot) 
    view = [mapView dequeueReusableAnnotationViewWithIdentifier:@"myAnnotationIdentifier"]; 
    if (!view) { 
     // Could not reuse a view ... 

     // Creating a new annotation view, in this case it still looks like a pin 
     view = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotationIdentifier"] autorelease]; 
     view.canShowCallOut = YES; // So that the callout can appear 

     UIImageView *myImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"someName"]]; 
     myImageView.frame = CGRectMake(0,0,31,31); // Change the size of the image to fit the callout 

     // Change this to rightCallout... to move the image to the right side 
     view.leftCalloutAccessoryView = myImageView; 
     [myImageView release], myImageView = nil; 
    } 
    } 
    return view; 
} 

然而,如果你想一切都表明大量的圖片,直接在地圖上(而不是在標註)上那麼你可以使用相同的委託方法設置註釋視圖的「圖像」屬性,如下所示:

-(MKAnnotationView*)mapView:(MKMapView)mapView viewForAnnotation:(id<MKAnnotation>)annotation { 
     // If you are showing the users location on the map you don't want to change it 
     MKAnnotationView *view = nil; 
     if (annotation != mapView.userLocation) { 
     // This is not the users location indicator (the blue dot) 
     view = [mapView dequeueReusableAnnotationViewWithIdentifier:@"myAnnotationIdentifier"]; 
     if (!view) { 
      // Could not reuse a view ... 

      // Creating a new annotation view 
      view = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotationIdentifier"] autorelease]; 

      // This will rescale the annotation view to fit the image 
      view.image = [UIImage imageNamed:@"someName"]; 
     } 
     } 
    return view; 
} 

我希望回答您的問題