在向注释添加自定义视图后,我无法获取我的标注视图。我究竟做错了什么?

时间:2018-01-18 09:55:26

标签: ios mapkit swift4

我在annotationview中添加了一个子视图并加载了一个customview引脚。我当前的问题是,当我点击我的注释引脚(即customView引脚)时,我无法查看我的标注视图。

我创建了一个标签并将其添加到detailCalloutAccessoryView,因为我的标注文本非常大。有没有办法显示这个标注

这是我的代码

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView?
    {
        var subTitlelbl = UILabel()
     //   subTitlelbl.text = "sri ganganagar this is my home twon.sri ganganagar this is my home twon.sri ganganagar this is my home twon.  "

        var width = NSLayoutConstraint(item: subTitlelbl, attribute: .width, relatedBy: .lessThanOrEqual, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 150)
        var height = NSLayoutConstraint(item: subTitlelbl, attribute: .height, relatedBy: .greaterThanOrEqual, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 0)
        subTitlelbl.numberOfLines = 0
        subTitlelbl.addConstraint(width)
        subTitlelbl.addConstraint(height)

        if annotation is MKUserLocation
        {
            return nil
        }

        var annotationView = self.appleMap.dequeueReusableAnnotationView(withIdentifier: "Pin") //as? SVPulsingAnnotationView
        var pulseView = self.appleMap.dequeueReusableAnnotationView(withIdentifier: "Pin1") as? SVPulsingAnnotationView

        if annotationView == nil || pulseView == nil
        {
            annotationView = AnnotationView(annotation: annotation, reuseIdentifier: "Pin") //as? SVPulsingAnnotationView
            pulseView = self.appleMap.dequeueReusableAnnotationView(withIdentifier: "Pin1") as? SVPulsingAnnotationView
            annotationView?.canShowCallout = true
            pulseView?.canShowCallout = true
        }
        else
        {
            annotationView?.annotation = annotation
            pulseView?.annotation = annotation

            annotationView?.canShowCallout = true
            pulseView?.canShowCallout = true
        }
        print("annotation.coordinate : ", annotation.coordinate.latitude.description)

        let myCustomView:CustomView = UINib(nibName: "CustomView", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! CustomView

        if ( if (annotation is IgnitionAnnotationView)
        {
            myCustomView.customMarkerIconImage.image = #imageLiteral(resourceName: "icon_idle")
            myCustomView.longArrowImage.image = nil
            myCustomView.customTruckImage.image = nil

//            var button = UIButton(type: UIButtonType.system) as! UIButton
//            button.frame = myCustomView.frame
//            button.backgroundColor = UIColor.clear
//            button.addTarget(self, action: "buttonAction:", for: UIControlEvents.touchUpInside)
//          // button.setTitle("selected", for: .normal)
//           // button.setTitleColor(UIColor.black, for: .normal)
//            button.contentHorizontalAlignment = UIControlContentHorizontalAlignment.left
//            myCustomView.addSubview(button)
//            button.tag = 2
            subTitlelbl.text = annotation.subtitle!
            print("(annotation.subtitle) = ",annotation.subtitle!)
            annotationView?.detailCalloutAccessoryView = subTitlelbl
            annotationView?.canShowCallout = true
  //     self.appleMap.selectAnnotation(annotation, animated: true)
        //   annotationView?.isSelected  = true
           // annotationView?.
        }

        else
        {
            myCustomView.customMarkerIconImage.image = #imageLiteral(resourceName: "green_custom_marker_icon")
            myCustomView.longArrowImage.image = #imageLiteral(resourceName: "east")
            myCustomView.customTruckImage.image = #imageLiteral(resourceName: "truck_east")

            let pulseHalo = SVPulsingAnnotationView()
            annotationView?.sendSubview(toBack: pulseHalo)

            if slideCurrentValue < 0.6
            {
                pulseHalo.delayBetweenPulseCycles = 3
                pulseHalo.outerColor = .green
                pulseHalo.annotationColor = .green
                pulseHalo.pulseScaleFactor = 20
                pulseHalo.pulseColor = .black
            }
            else
            {
                pulseHalo.delayBetweenPulseCycles = 0
                pulseHalo.outerColor = .green
                pulseHalo.annotationColor = .green
                pulseHalo.pulseScaleFactor = 1
                pulseHalo.pulseColor = .black
            }

            pulseHalo.frame = CGRect(x: 15, y: 20, width: 5, height: 5)
            annotationView?.addSubview(pulseHalo)
        }

        annotationView?.centerOffset = CGPoint(x: 0,y: (annotationView?.frame.size.height)!*(-0.5));
        annotationView?.addSubview(myCustomView)
        pulseView?.addSubview(myCustomView)
        pulseView?.centerOffset = CGPoint(x: -15,y: -38)

        annotationView?.bringSubview(toFront: myCustomView)
        annotationView?.centerOffset = CGPoint(x: -15,y: -38)

        return annotationView
    }
}

我的IgnitionAnnotationView是

class IgnitionAnnotationView:  NSObject, MKAnnotation
{
    var title: String?
    var subtitle: String?
    var latitude: Double
    var longitude:Double

    var coordinate: CLLocationCoordinate2D
    {
        return CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
    }

    init(latitude: Double, longitude: Double)
    {
        self.latitude = latitude
        self.longitude = longitude
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

2 个答案:

答案 0 :(得分:0)

在此处查看我的演示文稿,了解自定义标注和didSelect&amp;&amp; deSelect customPinAnnotationButton的实施

答案 1 :(得分:0)

实际关注https://github.com/robertmryan/CustomMapViewAnnotationCalloutSwift 这帮我解决了所有问题 谢谢