Swift - Add MKAnnotationView To MKMapView

后端 未结 3 643
野的像风
野的像风 2020-11-30 04:13

I\'m trying to add MKAnnotationView to MKMapView but I can\'t do it… Can anyone help me?

Here is my code:

override func vie         


        
3条回答
  •  天涯浪人
    2020-11-30 04:42

    1- your map view should delegate itself to your view controller 2- you should implement

    func mapView(aMapView: MKMapView!, viewForAnnotation annotation: CustomMapPinAnnotation!) -> MKAnnotationView!
    

    this function is called from every annotation added to your Map

    3- subclass your annotations to identify them in the viewForAnnotation method

    4- in viewForAnnotation, add

    if annotation.isKindOfClass(CustomMapPinAnnotation)
    {
      // change the image here
    var pinView = aMapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? YourSubclassedAnnotation
      pinView!.image = UIImage(contentsOfFile: "xyz")
    }
    

提交回复
热议问题