使自定义注释可以进行

时间:2016-08-03 02:17:10

标签: ios swift mapkit mapkitannotation

我不明白如何使我的自定义地图注释可用。

这是生成注释的代码:

import UIKit
import MapKit
import CoreLocation

class NeighbourhoodMapCell: UITableViewCell {

    var userList : NSMutableArray?
    var itemList : NSMutableArray?

    @IBOutlet weak var mapView: MKMapView!

    override func awakeFromNib() {
        super.awakeFromNib()
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }

    func loadAnnotations(){
        let allAnnotations = mapView.annotations
        mapView.removeAnnotations(allAnnotations)

        if (userList != nil){
            //load users
            if let userList = self.userList{
                for user in userList{
                    if let user = user as? User, let lat = user.latitude, let long = user.longitude{
                        let location = CLLocationCoordinate2D(latitude: lat, longitude: long)
                        let annotation = UserAnnotation(coordinate: location, user: user)
                        mapView.addAnnotation(annotation)
                    }
                }
            }

        } else {
            //load items
            if let itemList = self.itemList{
                for item in itemList{
                    if let item = item as? Item, let lat = item.latitude, let long = item.longitude{
                        let location = CLLocationCoordinate2D(latitude: lat, longitude: long)
                        let annotation = ItemAnnotation(coordinate: location, item: item)
                        mapView.addAnnotation(annotation)
                    }
                }
            }
        }
    }
}

extension NeighbourhoodMapCell: MKMapViewDelegate {
    func mapView(mapView: MKMapView, annotationView view: MKAnnotationView,
                 calloutAccessoryControlTapped control: UIControl) {
        print("1")
    }

    func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {
        print("2")
    }

    func calloutTapped(sender:UITapGestureRecognizer) {
        print("3")
    }

    func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
        guard !annotation.isKindOfClass(MKUserLocation) else {
            return nil
        }

        let annotationIdentifier = "AnnotationIdentifier"
        var annotationView: MKAnnotationView?

        if (annotation.isKindOfClass(UserAnnotation) == true){
            let annotation = annotation as? UserAnnotation

            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 {
                annotationView.canShowCallout = true
                annotationView.image = UIImage(named: "ic_annotation")

                let rect = CGRect(x: 5, y: 5, width: 36.0, height: 36.0)
                let annotationImageView = GFItemImageView(frame: rect)
                annotationImageView.image = nil
                annotationImageView.roundedImage = true
                annotationImageView.imageSize = .Small_100x100


                if let user = annotation!.user, imageURLString = user.imageURLString, imageURL = NSURL(string: imageURLString) {
                    annotationImageView.setImageWithUrl(imageURL, placeHolderImage: UIImage(named:"ic_profile_pic"))
                } else {
                    annotationImageView.image = UIImage(named:"ic_profile_pic")
                }
                annotationView.addSubview(annotationImageView)
            }
        }

        if (annotation.isKindOfClass(ItemAnnotation) == true){

            let annotation = annotation as? ItemAnnotation

            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 {
                annotationView.canShowCallout = true
                annotationView.image = UIImage(named: "ic_annotation")

                let rect = CGRect(x: 5, y: 5, width: 36.0, height: 36.0)
                let annotationImageView = GFItemImageView(frame: rect)
                annotationImageView.image = nil
                annotationImageView.roundedImage = true
                annotationImageView.imageSize = .Small_100x100

                if let item = annotation?.item, let itemImageURLString = item.imageURLString, let itemImageURL = NSURL(string: itemImageURLString){
                    annotationImageView.setImageWithUrl(itemImageURL, placeHolderImage: nil)
                } else {
                    annotationImageView.image = UIImage(named: "ic_item_pic_\(NSLocale.preferredLanguages()[0].containsString("zh") ? "zh" : "en")")
                }

                annotationView.addSubview(annotationImageView)
            }
        }
        return annotationView
    }
}

class UserAnnotation : NSObject, MKAnnotation {
    var user:User?
    var coordinate: CLLocationCoordinate2D
    var title: String?

    init(coordinate: CLLocationCoordinate2D, user: User?) {
        self.coordinate = coordinate
        self.user = user
    }
}

class ItemAnnotation : NSObject, MKAnnotation {
    var item:Item?
    var coordinate: CLLocationCoordinate2D
    var title: String?

    init(coordinate: CLLocationCoordinate2D, item:Item?) {
        self.coordinate = coordinate
        self.item = item
    }
}

这将产生以下视图:

enter image description here

然而,当我点击注释或我添加的图像时,没有任何事情发生。我希望它能够进入打印1,2或3的方法之一。

1 个答案:

答案 0 :(得分:0)

通过更改修复:

 annotationView.canShowCallout = true

要:

 annotationView.canShowCallout = false

点击注释现在将触发:

func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {
    print("2")
}