【问题标题】:MKMapView: Instead of Annotation Pin, a custom viewMKMapView:一个自定义视图,而不是 Annotation Pin
【发布时间】:2012-04-06 14:00:47
【问题描述】:

我想在我的MKMapView 中显示一张图片,而不是小rock pin

有人可以在这里放一些有用的代码,或者告诉怎么做吗?

-(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;
}

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

【问题讨论】:

  • 听起来你想要一个自定义注释。检查this tutorial。如果不适合您,谷歌搜索自定义注释教程也可以帮助您。
  • 我猜 Annotation 只是 note 用来告知关于 pinned 位置的信息。但我想自定义图钉,将其图像更改为公司徽标。
  • 我也尝试过你的tutorial方式,看来对我来说并不奏效。教程将MKAnnotationView 对象的image 属性设置为一些UIImage。这是我测试过的,不会将 pinView 更改为我分配的图像。
  • @turtle,你能发布你尝试过的代码吗,会发生什么以及你期望什么?
  • @AnnaKarenina :我已经编辑了我的问题,你可以看看代码。谢谢..

标签: ios objective-c swift mkmapview mkannotation


【解决方案1】:

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

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

所以把代码改成:

-(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;
}


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

如果您仍希望删除图像注释,则必须自己制作动画。您可以在 Stack Overflow 中搜索“animatesdrop mkannotationview”,您会找到几个答案。这是前两个:

【讨论】:

【解决方案2】:

这是关于 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
}

【讨论】:

  • 谢谢。你为什么打电话给annotationView?.annotation = annotation?我以为注解和视图是自动链接的?
  • 它们不会自动链接。您正在使可重用的注释视图出列。如果它之前使用过,它可能有另一个 annotation 链接。
【解决方案3】:

我同意 Anna 的回答,并且我喜欢在 swift3 中展示它的外观。这个回答有许多其他选项。比如调整图像大小,从中获取图像列表数组等。

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
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多