如何在MKMapView中为引脚添加标题?

时间:2011-09-08 08:19:49

标签: iphone mapkit

enter image description here

我有drop pin的代码。但我想在上面的图像中显示标题栏。如何添加星星图像和评论图像?

6 个答案:

答案 0 :(得分:0)

您可以使用自定义MKAnnotationView

答案 1 :(得分:0)

    - (MKAnnotationView *)mapView:(MKMapView *)mapViewTmp viewForAnnotation:(id<MKAnnotation>)annotation
    {
        if(annotation == mapView.userLocation)
            return nil;
        MKPinAnnotationView *pinView = (MKPinAnnotationView*)[mapViewTmp dequeueReusableAnnotationViewWithIdentifier:@"Pin"];
        if (pinView ==nil) {
            pinView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"Pin"];
            {

                if([addedAnns containsObject:annotation]){
                    MyAnnotation *a = [addedAnns objectAtIndex:[addedAnns indexOfObject:annotation]];
                    UIImage * image = [UIImage imageNamed:a.pinImage];
                    UIImageView *imageView = [[[UIImageView alloc] initWithImage:image] autorelease];
                    imageView.frame=[pinView bounds];
                    [pinView addSubview:imageView];
                    //[imageView release];
                    pinView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDeta`enter code here`ilDisclosure];
                    pinView.rightCalloutAccessoryView.tag = a.iAnnId;
                    [(UIButton *)pinView.rightCalloutAccessoryView addTarget:self action:@selector(openSpot:) forControlEvents:UIControlEventTouchUpInside];


                    pinView.enabled = YES;
                    pinView.centerOffset = CGPointMake(0,-15);
                    pinView.calloutOffset = CGPointMake(-8,0);
                    pinView.canShowCallout = YES;
                }
            }
            pinView.animatesDrop = YES;     
        } 
        return pinView; // we cant release Or Auto release this object. Due To it will use futher
    }





// The left accessory view to be used in the standard callout.
                    @property (retain, nonatomic) UIView *leftCalloutAccessoryView;

                    // The right accessory view to be used in the standard callout.
                    @property (retain, nonatomic) UIView *rightCalloutAccessoryView;

如您所见,我正在向rightCalloutAccessoryView添加按钮。同样,您可以添加图像。

答案 2 :(得分:0)

使用此委托方法

 -(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;
    }

答案 3 :(得分:0)

与TableView中的自定义单元格相同。 您只需要创建MKAnnotationView的新子类,并绘制视图您想要的。

答案 4 :(得分:0)

否则,您可以尝试使用本教程来获取自定义标注:http://dev.tuyennguyen.ca/?p=298

答案 5 :(得分:0)

为此,您必须将图像放入注释中以使其自定义,我认为James Rantanen撰写的这篇文章http://blog.asolutions.com/2010/09/building-custom-map-annotation-callouts-part-1可以帮助您。