2015-09-17 80 views
32

我的代码在iOS 7到8下工作正常。昨天更新了我的针脚上的自定义图像被标准针脚图像取代。 有什么建议吗?MapKit没有在iOS9上显示自定义注释图像

我的代码:

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 
} 

没有地图上看起来是这样的: ios9 without images

回答

70

代替创建一个MKPinAnnotationView,创建一个普通的MKAnnotationView

MKPinAnnotationView子类由于设计为仅显示标准红色,绿色和紫色引脚(通过pinColor属性),因此会忽略图像属性。

当您切换到MKAnnotationView,你必须注释掉animatesDrop线,以及因为该属性是特定于MKPinAnnotationView

+0

我有相同的问题。您建议的解决方案为我解决了它,谢谢。 – Perex19

+0

这个解决方案也适用于我。谢谢! –

+0

谢谢!那太棒了。 – Natasha

3

下面的代码工作完全在所有的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; 
}