在swift中向MapKit注释视图添加操作

时间:2021-02-10 21:16:51

I am getting a problem with my annotation views on map. I am showing my all pets on map as custom with their picture. When I click any annotation, I want to go chat screen. Its identifier "chatView". I guess my code is true. But it is not working. Even, It is not printing out "Button tapped". How can I fix this problem. Here is the code

我在地图上的注释视图出现问题。我在地图上显示我的所有宠物与他们的照片定制。当我点击任何注释时,我想去聊天屏幕。其标识符为“chatView”。我想我的代码是真的。但它没有用。甚至,它没有打印出“按钮敲击”。我该如何解决这个问题。这是代码

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {

    if !(annotation is MKPointAnnotation) {
        return nil
    }
    var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "petIdentifier")
    if annotationView == nil {
        annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "petIdentifier")
        annotationView!.canShowCallout = true
    }else {
        annotationView!.annotation = annotation
    }

    let pannotation : PPointAnnotation = annotation as! PPointAnnotation
    let petImage : UIImageView = UIImageView()
    petImage.frame = CGRect(x: -16, y: -4, width: 50, height: 50)
    petImage.layer.cornerRadius = 16
    petImage.layer.masksToBounds = true
    petImage.clipsToBounds = true
    petImage.backgroundColor = UIColor.white
    petImage.sd_setImage(with: URL(string: pannotation.photoURL as String), placeholderImage: UIImage(named: "map-e-giden.png"))
    annotationView?.addSubview(petImage)
    annotationView?.bringSubview(toFront: petImage)

    let button = UIButton(type: .detailDisclosure)
    annotationView?.rightCalloutAccessoryView = button
    return annotationView
}

func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!, calloutAccessoryControlTapped control: UIControl!) {
    if control == view.rightCalloutAccessoryView{
        print("button tapped")
        performSegue(withIdentifier: "chatView", sender: view)
    }
}

1 个解决方案

#1


0  

You should not use addSubview to show image on annotation.

您不应该使用addSubview在注释上显示图像。

e.g.

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    if annotation is MKUserLocation {
        return nil
    }

    let reuseId = "petIdentifier"
    var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId)
    if pinView == nil {
        pinView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        pinView?.canShowCallout = true
        pinView?.image = (downloaded image here) // e.g. SDWebImageManager.shared().loadImage(with: ...

        let rightButton: AnyObject! = UIButton(type: UIButtonType.detailDisclosure)
        pinView?.rightCalloutAccessoryView = rightButton as? UIView
    }
    else {
        pinView?.annotation = annotation
    }

    return pinView
}

#1


0  

You should not use addSubview to show image on annotation.

您不应该使用addSubview在注释上显示图像。

e.g.

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    if annotation is MKUserLocation {
        return nil
    }

    let reuseId = "petIdentifier"
    var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId)
    if pinView == nil {
        pinView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        pinView?.canShowCallout = true
        pinView?.image = (downloaded image here) // e.g. SDWebImageManager.shared().loadImage(with: ...

        let rightButton: AnyObject! = UIButton(type: UIButtonType.detailDisclosure)
        pinView?.rightCalloutAccessoryView = rightButton as? UIView
    }
    else {
        pinView?.annotation = annotation
    }

    return pinView
}