Ios MKMapView:自定义视图,而不是注释接点

Ios MKMapView:自定义视图,而不是注释接点,ios,objective-c,swift,mkmapview,mkannotation,Ios,Objective C,Swift,Mkmapview,Mkannotation,我想在我的MKMapView中显示图像,而不是小石柱图钉 有人可以在这里输入一些有用的代码,或者告诉我们怎么做 -(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation: (id <MKAnnotation>)annotation { MKPinAnnotationView *pinView = nil; if(annotation != mapView.userLocation)

我想在我的
MKMapView
中显示图像,而不是小石柱图钉

有人可以在这里输入一些有用的代码,或者告诉我们怎么做

-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:
    (id <MKAnnotation>)annotation {
    MKPinAnnotationView *pinView = nil; 
    if(annotation != mapView.userLocation) 
    {
        static NSString *defaultPinID = @"com.invasivecode.pin";
        pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
        if ( pinView == nil ) pinView = [[MKPinAnnotationView alloc]
                                          initWithAnnotation:annotation reuseIdentifier:defaultPinID];

        pinView.pinColor = MKPinAnnotationColorGreen; 
        pinView.canShowCallout = YES;
        pinView.animatesDrop = YES;
        pinView.image = [UIImage imageNamed:@"pinks.jpg"]; //as suggested by Squatch
    } 
    else {
        [mapView.userLocation setTitle:@"I am here"];
    }
    return pinView;
}
-(MKAnnotationView*)地图视图:(MKMapView*)mV视图用于注释:
(id)注释{
MKPinAnnotationView*pinView=nil;
if(注释!=mapView.userLocation)
{
静态NSString*defaultPinID=@“com.invasivecode.pin”;
pinView=(MKPinAnnotationView*)[mapView出列重用AnnotationViewWithIdentifier:defaultPinID];
如果(pinView==nil)pinView=[[MKPinAnnotationView alloc]
initWithAnnotation:注释重用标识符:defaultPinID];
pinView.pinColor=MKPinAnnotationColorGreen;
pinView.canShowCallout=是;
pinView.animatesDrop=是;
pinView.image=[UIImage ImageName:@“pinks.jpg”];//如Shuckh所建议
} 
否则{
[mapView.userLocation setTitle:@“我在这里”];
}
返回pinView;
}

我希望我的图像pinks.jpg出现在地图上,固定位置而不是默认的pin视图(岩石pin形状)。但是我仍然得到了pin的默认图像。

当您想将自己的图像用于注释视图时,应该创建
MKAnnotationView
而不是
MKPinAnnotationView

MKPinAnnotationView
MKAnnotationView
的子类,因此它有一个
image
属性,但它通常会覆盖该属性并绘制一个pin图像(这就是它的用途)

因此,将代码更改为:

-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation 
{
    MKAnnotationView *pinView = nil; 
    if(annotation != mapView.userLocation) 
    {
        static NSString *defaultPinID = @"com.invasivecode.pin";
        pinView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
        if ( pinView == nil ) 
            pinView = [[MKAnnotationView alloc]
                                         initWithAnnotation:annotation reuseIdentifier:defaultPinID];

        //pinView.pinColor = MKPinAnnotationColorGreen; 
        pinView.canShowCallout = YES;
        //pinView.animatesDrop = YES;
        pinView.image = [UIImage imageNamed:@"pinks.jpg"];    //as suggested by Squatch
    } 
    else {
        [mapView.userLocation setTitle:@"I am here"];
    }
    return pinView;
}
-(MKAnnotationView*)地图视图:(MKMapView*)mV视图用于注释:(id)注释
{
MKAnnotationView*pinView=nil;
if(注释!=mapView.userLocation)
{
静态NSString*defaultPinID=@“com.invasivecode.pin”;
pinView=(MKAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
如果(pinView==nil)
pinView=[[MKAnnotationView alloc]
initWithAnnotation:注释重用标识符:defaultPinID];
//pinView.pinColor=MKPinAnnotationColorGreen;
pinView.canShowCallout=是;
//pinView.animatesDrop=是;
pinView.image=[UIImage ImageName:@“pinks.jpg”];//如Shuckh所建议
} 
否则{
[mapView.userLocation setTitle:@“我在这里”];
}
返回pinView;
}

请注意,
animatesDrop
也被注释掉,因为该属性仅存在于
MKPinAnnotationView

如果仍然希望删除图像批注,则必须自己制作动画。您可以在堆栈溢出中搜索“animatesdrop mkannotationview”,您将找到几个答案。以下是前两个:


    • 以下是关于Swift 3的答案。如果可能,它将退出注释视图,如果没有,则创建一个新的注释视图:

      func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
          // Don't want to show a custom image if the annotation is the user's location.
          guard !(annotation is MKUserLocation) else {
              return nil
          }
      
          // Better to make this class property
          let annotationIdentifier = "AnnotationIdentifier"
      
          var annotationView: MKAnnotationView?
          if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier) {
              annotationView = dequeuedAnnotationView
              annotationView?.annotation = annotation
          }
          else {
              annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
              annotationView?.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
          }
      
          if let annotationView = annotationView {
              // Configure your annotation view here
              annotationView.canShowCallout = true
              annotationView.image = UIImage(named: "yourImage")
          }
      
          return annotationView
      }
      
      Swift 2.2

      func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
          // Don't want to show a custom image if the annotation is the user's location.
          guard !annotation.isKindOfClass(MKUserLocation) else {
              return nil
          }
      
          // Better to make this class property
          let annotationIdentifier = "AnnotationIdentifier"
      
          var annotationView: MKAnnotationView?
          if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(annotationIdentifier) {
              annotationView = dequeuedAnnotationView
              annotationView?.annotation = annotation
          }
          else {
              let av = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
              av.rightCalloutAccessoryView = UIButton(type: .DetailDisclosure)
              annotationView = av
          }
      
          if let annotationView = annotationView {
              // Configure your annotation view here
              annotationView.canShowCallout = true
              annotationView.image = UIImage(named: "yourImage")
          }
      
          return annotationView
      }
      

      我同意Anna的回答,我想展示一下在swift3中的效果。这个答案还有很多其他选项。比如调整图像大小,从数组和ect中获取图像列表

      func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
              if let annotation = annotation as? PetrolStation {
                  let identifier = "pinAnnotation"
                  var view: MKAnnotationView
                  if let dequeuedView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
                      as? MKPinAnnotationView { // 2
                      dequeuedView.annotation = annotation
                      view = dequeuedView
                  } else {
                      // 3
                      view = MKAnnotationView(annotation: annotation, reuseIdentifier: identifier)
                      view.canShowCallout = true
      
                      //here We put a coordinates where we like to show bubble with text information up on the pin image
                      view.calloutOffset = CGPoint(x: -7, y: 7)
      
      
                      //Here this is a array of images
                      let pinImage = PetrolItem[activePlace].imgPetrol?[activePlace]
      
                      //Here we set the resize of the image
                      let size = CGSize(width: 30, height: 30)
                      UIGraphicsBeginImageContext(size)
                      pinImage?.draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height))
                      let resizeImage = UIGraphicsGetImageFromCurrentImageContext()
                      UIGraphicsEndImageContext()
                      view.image = resizeImage
      
                      //Here we like to put into bubble window a singe for detail Informations
                      view.rightCalloutAccessoryView = UIButton(type: .detailDisclosure) as UIView
                     //Here we make change of standard pin image with our image
                      view.image = resizeImage
                  }
      
                  return view
              }
              return nil
          }
      

      听起来您需要自定义注释。退房如果自定义注释教程不适合你的话,用谷歌搜索它也会让你上路。我想注释只是用来告诉你固定位置信息的注释。但我想自定义pin,将其图像更改为公司徽标。我也尝试过你的教程方式,似乎对我不起作用。本教程正在将
      MKAnnotationView
      对象的
      image
      属性设置为某些
      UIImage
      。这是我测试过的,不会将pinView更改为我指定的图像。@turtle,你能发布你尝试过的代码吗,发生了什么,你期望什么吗?@AnnaKarenina:我已经编辑了我的问题,你可以查看代码。谢谢..嗨Anna,我只想在点击此pin图像时添加一个事件。我该怎么做?嗨,Anna,你对下面的问题有什么想法吗@Anna请帮我解决这个问题Anna,你的
      if
      语句需要一个
      else
      子句,否则重复使用的PIN将不会使用
      注释。例如,
      if(pinView==nil){…}else{pinView.annotation=annotation;}
      。谢谢。为什么要调用
      annotationView?.annotation=annotation
      ?我以为注释和视图是自动链接的?它们不是自动链接的。您正在对可重用注释视图进行排队。如果在使用之前,它可能有另一个
      注释
      链接。