【问题标题】:MapKit not showing custom Annotation pin image on iOS9MapKit 未在 iOS9 上显示自定义注释图钉图像
【发布时间】:2015-12-14 12:08:31
【问题描述】:

我的代码从 iOS 7 到 8 运行良好。随着昨天的更新,我的 pin 上的自定义图像被标准 pin 图像取代。 有什么建议吗?

我的代码:

extension ViewController: MKMapViewDelegate {

    func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView! {

        if annotation is MKUserLocation {
            return nil
        }

        let reuseId = String(stringInterpolationSegment: annotation.coordinate.longitude)

        var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView

        if pinView == nil {

            pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
            pinView!.canShowCallout = true
            pinView!.image = getRightImage(annotation.title!!) 
        }

        let button = UIButton(type: UIButtonType.DetailDisclosure) 
        pinView?.rightCalloutAccessoryView = button

        return pinView
    }
}

获取图像的函数根据名称返回UIImage:

func getRightImage (shopName:String)-> UIImage{

    var correctImage = UIImage()

    switch shopName
    {
    case "Kaisers":
        correctImage = UIImage(named: "Kaisers.jpg")!
    default:
        correctImage = UIImage(named: "sopiconsmall.png")!

    }

    return correctImage
}

不,地图是这样的:

【问题讨论】:

    标签: mkmapview mapkit mkannotation ios9 mkannotationview


    【解决方案1】:

    不要创建 MKPinAnnotationView,而是创建一个普通的 MKAnnotationView。

    MKPinAnnotationView 子类倾向于忽略 image 属性,因为它旨在仅显示标准的红色、绿色、紫色引脚(通过 pinColor 属性)。

    当您切换到 MKAnnotationView 时,您还必须注释掉 animatesDrop 行,因为该属性特定于 MKPinAnnotationView。

    【讨论】:

    • 我遇到了完全相同的问题。您建议的解决方案为我解决了这个问题,谢谢。
    • 谢谢!太好了。
    • 效果很好。感谢您的解决方案。
    • 伙计,你拯救了我的夜晚!我遇到了一个疯狂的问题,即仅在我第二次打开地图后才加载自定义图钉图像。并更改为MKAnnotationView 解决了问题!谢谢!
    【解决方案2】:

    以下代码可在所有 iOS 6 到 iOS 9 设备上完美运行:

        - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
    {
        // create a proper annotation view, be lazy and don't use the reuse identifier
        MKAnnotationView *view = [[MKAnnotationView alloc] initWithAnnotation:annotation
                                                                    reuseIdentifier:@"identifier"];
    
        // create a disclosure button for map kit
        UIButton *disclosure = [UIButton buttonWithType:UIButtonTypeContactAdd];
    
        [disclosure addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self
                                                                                 action:@selector(disclosureTapped)]];
        view.rightCalloutAccessoryView = disclosure;
    
           view.enabled = YES;
           view.image = [UIImage imageNamed:@"map_pin"];
    
    
        return view;
    }
    

    【讨论】:

      【解决方案3】:

      对于 Swift 4

      func mapView(mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
      
          if annotation is MKUserLocation {
              return nil
          }
      
          let reuseId = String(stringInterpolationSegment: annotation.coordinate.longitude)
      
          var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId)
      
          if pinView == nil {
      
              pinView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
              pinView!.canShowCallout = true
              pinView!.image = getRightImage(annotation.title!!) 
          }
      
          let button = UIButton(type: UIButtonType.DetailDisclosure) 
          pinView?.rightCalloutAccessoryView = button
      
          return pinView
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-02-28
        • 2018-12-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多