swift - 如何获取 Pin 图来更改其图像?

标签 swift mapkit

我有问题,我的注释仍然是一个图钉!我想要按钮和图片,以显示有关用户的信息并且它有效。但它不会改变 map 上图钉的外观! 这是我的注释 View :

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
    // 1
    let identifier = "pinAnnotation"

    // 2
    if annotation.isKindOfClass(pinAnnotation.self) {
        // 3
        var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier)

        if annotationView == nil {


            //4
            annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
            annotationView!.canShowCallout = true


            // 5
            let btn = UIButton(type: .InfoDark)
            annotationView!.rightCalloutAccessoryView = btn



            resultImgArray[0].getDataInBackgroundWithBlock {
                (imageData: NSData?, error: NSError?) -> Void in

                if error == nil {

                    let imageView = UIImageView(frame:CGRectMake(0,0,45,45))
                    imageView.image = UIImage(data: imageData!)
                    annotationView!.leftCalloutAccessoryView = imageView

                }


            }


        } else {
            // 6
            annotationView!.annotation = annotation
        }

        return annotationView
    }

    return nil

}

这是我的 pinAnnotationView.swift 作为 MKAnnotationView:

import UIKit
import MapKit
import CoreLocation

class pinAnnotationView: MKAnnotationView {

    override init(frame :CGRect) {
        super.init(frame: frame)
    }

    override init(annotation: MKAnnotation?, reuseIdentifier: String?) {

        super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)

        var frame = self.frame
        frame.size = CGSizeMake(80, 80)
        self.frame = frame
        self.backgroundColor = UIColor.clearColor()
        self.centerOffset = CGPointMake(-5, -5)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }


    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func drawRect(rect: CGRect) {
        // Drawing code

        UIImage(named: "carlogo.png")?.drawInRect(CGRectMake(30, 30, 30, 30))

    }
}

这是图片
enter image description here

最佳答案

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        if annotation.isMember(of: MKUserLocation.self) {
            return nil
        }
        let identifier = "someID"
        var dequeuedAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
        if dequeuedAnnotationView == nil{
            dequeuedAnnotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: identifier)
            configureDetailView(annotationView: dequeuedAnnotationView!)

        }
        return dequeuedAnnotationView
    }    

func configureDetailView(annotationView: MKAnnotationView) {
            var frame = self.frame
            frame.size = CGSizeMake(80, 80)
            self.frame = frame
            self.backgroundColor = UIColor.clearColor()
            self.centerOffset = CGPointMake(-5, -5)
            annotationView.image = UIImage(named:"yourImage")       
    }

并使用annotationView.detailCalloutAccessoryView = "yourView"在这种情况下将是框架。

希望对你有用

关于swift - 如何获取 Pin 图来更改其图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36285208/

相关文章:

ios - 想要 : How to reliably, 始终选择一个 MKMapView 注释

mapkit - calloutAccessoryControlTapped 未调用/触发

ios - 获取当前事件的键盘语言作为 NSLocale

ios - 如何在 Swift 中将 UInt64.max 转换为字符串?

swift - var Double 类型 swift 崩溃

swift - 自定义注释调用

swift - centralManager didConnectPeripheral 从未被调用

Swift 所需参数逻辑

swift - 如何为 MKPolyline 和 MKPolygon 添加描述?

ios - 使用 Apple Map Kit 从某个位置获取周围的邮政编码