MapKit没有在iOS9上显示自定义注释图钉图像

Fel*_*ber 35 mapkit mkmapview mkannotation mkannotationview ios9

从iOS 7到8,我的代码运行良好.昨天更新了我的引脚上的自定义图像被标准引脚图像替换.有什么建议?

我的代码:

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
    }
}
Run Code Online (Sandbox Code Playgroud)

获取图像的函数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
}
Run Code Online (Sandbox Code Playgroud)

没有地图看起来像这样: ios9没有图像

小智 77

而不是创建一个MKPinAnnotationView,创建一个平原MKAnnotationView.

MKPinAnnotationView子类往往因为它的设计,只会显示标准的红,绿,紫引脚(通过pinColor属性)忽略图像属性.

切换到时MKAnnotationView,您必须注释掉animatesDrop行,因为该属性是特定的MKPinAnnotationView.


Har*_*ani 5

以下代码可在所有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;
}
Run Code Online (Sandbox Code Playgroud)