Swift 用于在图像下显示图像和文本的自定义类MKAnnotation
我为MKAnnotation实现了一个自定义类。不幸的是,我仍然不知道如何在图像下显示文本?我也试过使用mkmarkernotationview,一切正常,但不幸的是,您无法在那里更改图像。我需要更改注释的图像和文本。请帮忙!我怎样才能解决我的问题Swift 用于在图像下显示图像和文本的自定义类MKAnnotation,swift,xcode,Swift,Xcode,我为MKAnnotation实现了一个自定义类。不幸的是,我仍然不知道如何在图像下显示文本?我也试过使用mkmarkernotationview,一切正常,但不幸的是,您无法在那里更改图像。我需要更改注释的图像和文本。请帮忙!我怎样才能解决我的问题 class ImageAnnotation : NSObject, MKAnnotation { var coordinate: CLLocationCoordinate2D var title: String? var su
class ImageAnnotation : NSObject, MKAnnotation {
var coordinate: CLLocationCoordinate2D
var title: String?
var subtitle: String?
var image: UIImage?
var colour: UIColor?
var id : Int?
var type: String!
override init() {
self.coordinate = CLLocationCoordinate2D()
self.title = nil
self.subtitle = nil
self.image = nil
self.colour = UIColor.white
}
}
class ImageAnnotationView: MKAnnotationView {
private var imageView: UIImageView!
override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
self.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
self.imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
self.addSubview(self.imageView)
self.imageView.layer.cornerRadius = 5.0
self.imageView.layer.masksToBounds = true
}
override var image: UIImage? {
get {
return self.imageView.image
}
set {
self.imageView.image = newValue
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation.isKind(of: MKUserLocation.self) {
return nil
}
if !annotation.isKind(of: ImageAnnotation.self) { //Handle non-ImageAnnotations..
var pinAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "DefaultPinView")
if pinAnnotationView == nil {
pinAnnotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "DefaultPinView")
}
return pinAnnotationView
}
var view: ImageAnnotationView? = mapView.dequeueReusableAnnotationView(withIdentifier: "imageAnnotation") as? ImageAnnotationView
if view == nil {
view = ImageAnnotationView(annotation: annotation, reuseIdentifier: "imageAnnotation")
}
let annotation = annotation as! ImageAnnotation
view?.image = UIImage(named: "point8")
view?.annotation = annotation
return view
}