Iphone 如何在MKMapView中为pin添加标题?

Iphone 如何在MKMapView中为pin添加标题?,iphone,mapkit,Iphone,Mapkit,我有别针的密码。但我想显示标题栏,如上图所示。如何添加星形图像并查看图像?您可以使用自定义的MKAnnotationView-(MKAnnotationView*)地图视图:(mkmappview*)地图视图tmp视图进行注释:(id)注释 - (MKAnnotationView *)mapView:(MKMapView *)mapViewTmp viewForAnnotation:(id<MKAnnotation>)annotation { if(a


我有别针的密码。但我想显示标题栏,如上图所示。如何添加星形图像并查看图像?

您可以使用自定义的
MKAnnotationView

-(MKAnnotationView*)地图视图:(mkmappview*)地图视图tmp视图进行注释:(id)注释
    - (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;
{ if(注释==mapView.userLocation) 返回零; MKPinAnnotationView*pinView=(MKPinAnnotationView*)[mapViewTmp dequeueReusableAnnotationViewWithIdentifier:@“Pin]”; 如果(pinView==nil){ pinView=[[MKPinAnnotationView alloc]initWithAnnotation:annotation重用标识符:@“Pin”]; { if([AddedAns containsObject:annotation]){ MyAnnotation*a=[Addedans对象索引:[Addedans索引对象:注释]]; UIImage*image=[UIImage ImageName:a.pinImage]; UIImageView*imageView=[[UIImageView alloc]initWithImage:image]自动释放]; frame=[pinView界限]; [pinView添加子视图:imageView]; //[图像视图发布]; pinView.rightCalloutAccessoryView=[UIButton buttonWithType:UIButtonTypeData`在此处输入代码`ilDisclosure]; pinView.rightCalloutAccessoryView.tag=a.iAnnId; [(UIButton*)pinView.rightCalloutAccessoryView添加目标:自操作:@selector(openSpot:)forControlEvents:UIControlEventTouchUpInside]; pinView.enabled=是; pinView.centerOffset=CGPointMake(0,-15); pinView.calloutOffset=CGPointMake(-8,0); pinView.canShowCallout=是; } } pinView.animatesDrop=是; } return pinView;//我们无法释放或自动释放此对象。因为它将进一步使用 } //要在标准详图索引中使用的左侧附件视图。 @属性(保留,非原子)UIView*leftCalloutAccessoryView; //要在标准详图索引中使用的右侧附件视图。 @属性(保留,非原子)UIView*rightCalloutAccessoryView;
正如您所看到的,我正在将按钮添加到
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*)地图视图:(MKMapView)地图视图注释:(id)注释{
//如果您在地图上显示用户的位置,则不想更改它
MKAnnotationView*view=nil;
if(注释!=mapView.userLocation){
//这不是用户位置指示器(蓝点)
视图=[mapView dequeueReusableAnnotationViewWithIdentifier:@“myAnnotationIdentifier”];
如果(!视图){
//无法重用视图。。。
//创建新的注释视图时,在本例中它看起来仍然像一个图钉
视图=[[MKPinAnnotationView alloc]initWithAnnotation:annotation重用标识符:@“myAnnotationIdentifier”]自动删除];
view.canShowCallOut=YES;//以便可以显示调用
UIImageView*myImageView=[[UIImageView alloc]initWithImage:[UIImageName:@“someName”]];
myImageView.frame=CGRectMake(0,0,31,31);//更改图像的大小以适合标注
//将此更改为rightCallout…以将图像移到右侧
view.leftCalloutAccessoryView=myImageView;
[myImageView发布],myImageView=nil;
}
}
返回视图;
}

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

否则,您可以尝试使用本教程获取自定义标注:

要做到这一点,您必须在注释中添加图像以使其自定义,我认为James Rantanen的这篇文章可以帮助您解决此问题。

您想在注释中添加图像,请查看此链接查看此链接,您将获得解决方案。