2011-02-10 176 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; 
} 

我希望回答您的问题