如何在地图中的引脚 select 处显示 MKAnnotation 视图

How to do MKAnnotation view show at pin select in map

当我单击某个图钉时无法打开我的 MKAnnotation 视图时,我遇到了问题。这是我的代码。请检查这个并给我一些解决方案。

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {

    if let annotationTitle = view.annotation?.title
    {
        let annotationIdentifier = "AnnotationIdentifier"
        var annotationView = MKAnnotationView()

        annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
        annotationView.annotation = annotation

        let dictList = self.arrPin.object(at: annotationView.tag) as? NSDictionary


        viewPopUp = UIView(frame: CGRect(x: (annotationView.frame.origin.x) - 128, y: (annotationView.frame.origin.y) - 59, width: 250, height: 60))



        let lblCategory = UILabel(frame: CGRect(x: imageView.frame.origin.x + 72, y: lblName.frame.origin.y + 25, width: imageView.frame.size.width - 30, height: 18))
        lblCategory.textColor = UIColor(red: 151/255, green: 151/255, blue: 151/255, alpha: 1.0)
        lblCategory.textAlignment = .natural
        lblCategory.font = UIFont(name: "HelveticaNeue-Light", size: 14)
        lblCategory.text = txtSearch.text


        viewPopUp.addSubview(lblCategory)

        annotationView.addSubview(viewPopUp)
    }
}

我已经在地图中打开了正确的注释视图。给注解框和select pin,那个时候pop view frame给定了。

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

        guard !(annotation is MKUserLocation) else {
            return nil
        }


        // Better to make this class property
        let annotationIdentifier = "AnnotationIdentifier"
        var annotationView = MKAnnotationView()
        annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
        annotationView.frame = CGRect(x: annotationView.frame.origin.x, y: annotationView.frame.origin.y, width: 80, height: 200)
        annotationView.annotation = annotation
        annotationView.tag = index
        index += 1

        let imageViewPin = UIImageView(frame: CGRect(x: 0, y: 0, width: 30, height: 40))
        imageViewPin.center = CGPoint(x: annotationView.center.x, y: annotationView.center.y - 11)
        imageViewPin.image = UIImage(named: "green_pin")
        annotationView.addSubview(imageViewPin)

        return annotationView
    }

    func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {

        debugPrint(view.tag)
        let overlays = self.mapVW.overlays
        self.mapVW.removeOverlays(overlays)


        if viewPopUp != nil {
            viewPopUp.removeFromSuperview()
            for item in viewPopUp.subviews
            {
                item.removeFromSuperview()
            }
        }

        if arrPin.count >= view.tag {
            let dictList = self.arrPin.object(at: view.tag) as? NSDictionary
            viewPopUp = UIView(frame: CGRect(x: -98, y:  10, width: 250, height: 60))
            view.addSubview(viewPopUp)

            let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: viewPopUp.frame.size.width, height: viewPopUp.frame.size.height))
            imageView.image = UIImage(named: "ic_mappopupbackground")
            imageView.contentMode = .scaleAspectFit

            let imgCar = UIImageView(frame: CGRect(x: imageView.frame.origin.x + 25, y: 9, width: 30, height: 20))
            imgCar.image = UIImage(named: "ic_car")
            imgCar.contentMode = .scaleAspectFit


            let lblMin = UILabel(frame: CGRect(x: imageView.frame.origin.x + 18, y: 33, width: 45, height: 15))
            lblMin.textColor = UIColor.white
            lblMin.textAlignment = .center
            lblMin.font = UIFont(name: "HelveticaNeue-Light", size: 11)

            let totalDist = distance(lat: dictList?.value(forKey: "lat") as! String, long: dictList?.value(forKey: "lon") as! String)
            lblMin.text = "\(totalDist) mi"

            let lblName = UILabel(frame: CGRect(x: imageView.frame.origin.x + 70, y: 5, width: 150, height: 25))
            lblName.textColor = UIColor.black
            lblName.textAlignment = .natural
            lblName.adjustsFontSizeToFitWidth  = true
            lblName.numberOfLines = 0
            lblName.lineBreakMode = .byCharWrapping
            lblName.font = UIFont(name: "HelveticaNeue-CondensedBold", size: 18)
            lblName.text = dictList?.value(forKey: "name") as? String

            let lblCategory = UILabel(frame: CGRect(x: imageView.frame.origin.x + 72, y: lblName.frame.origin.y + 25, width: imageView.frame.size.width - 30, height: 18))
            lblCategory.textColor = UIColor(red: 151/255, green: 151/255, blue: 151/255, alpha: 1.0)
            lblCategory.textAlignment = .natural
            lblCategory.font = UIFont(name: "HelveticaNeue-Light", size: 14)
            lblCategory.text = txtSearch.text


            viewPopUp.addSubview(imageView)
            viewPopUp.addSubview(imgCar)
            viewPopUp.addSubview(lblMin)
            viewPopUp.addSubview(lblName)
            viewPopUp.addSubview(lblCategory)
           // viewPopUp.addSubview(btnRoute)

            viewPopUp.isUserInteractionEnabled = true
            let tap = UITapGestureRecognizer(target: self, action: #selector(btnRouteView(sender:)))
            self.selectIndex = view.tag
            viewPopUp.addGestureRecognizer(tap)
        }

    }