我已经制作了自定义注释和自定义注释视图但由于某种原因未显示标注。
class MyAnnotation: NSObject,MKAnnotation {
var coordinate :CLLocationCoordinate2D
var title :String?
var subtitle: String?
init(coordinate :CLLocationCoordinate2D, title :String?, subtitle :String?) {
self.coordinate = coordinate
self.title = title
self.subtitle = subtitle
}
}
注释视图
class MyAnnotationView: MKAnnotationView {
override init(frame: CGRect) {
super.init(frame: frame)
}
override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
setup()
}
func setup() {
self.canShowCallout = true
let logo = UIImage(named: "logo.png")
let size = CGSize(width: 100, height: 100)
UIGraphicsBeginImageContext(size)
logo?.drawInRect(CGRectMake(0, 0, size.width, size.height))
let resizedImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
let logoImageView = UIImageView(image: resizedImage)
logoImageView.userInteractionEnabled = true
self.addSubview(logoImageView)
}
填充注释
private func populateAnnotations() {
for location in self.locations {
let annotation = MyAnnotation(coordinate: CLLocationCoordinate2D(latitude: location.latitude, longitude: location.longitude), title: location.name, subtitle: "nothing")
self.mapView.addAnnotation(annotation)
}
}
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation {
return nil
}
let annotationView = MyAnnotationView(annotation: annotation, reuseIdentifier: "AnnotationView")
return annotationView
}
当我点击自定义注释时,它不会显示标注。