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

时间:2015-09-17 14:52:16

标签: mkmapview mapkit mkannotation ios9 mkannotationview

我的代码在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
    }
}

获取图像的功能根据名称返回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

3 个答案:

答案 0 :(得分:75)

创建一个简单的 MKPinAnnotationView ,而不是创建 MKAnnotationView

MKPinAnnotationView 子类往往会忽略图像属性,因为它只是为了显示标准的红色,绿色,紫色图钉(通过pinColor属性)。

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

答案 1 :(得分: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;
}

答案 2 :(得分:0)

对于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
}