繁体   English   中英

使自定义注释成为可点击的

[英]Make Custom Annotations tappable

我不了解如何使我的自定义地图注释可显示。

这是生成注释的代码:

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

这将导致以下视图:

在此处输入图片说明

但是,当我单击注释或添加到顶部的图像时,没有任何反应。 我希望它能进入打印1、2或3的方法之一。

通过更改固定:

 annotationView.canShowCallout = true

至:

 annotationView.canShowCallout = false

点击注释现在将触发:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM