Custom pin image in annotationView in iOS

前端 未结 3 1946
一个人的身影
一个人的身影 2020-12-07 21:14

I\'m trying to change from Swift 1.2 to Swift 2.0 and I\'m at the end of the changes. Currently I\'m making changes in the MapViewController, and there isn\'t any error or w

相关标签:
3条回答
  • 2020-12-07 21:50

    Here is the answer:

    func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
    
        let identifier = "MyPin"
    
        if annotation.isKindOfClass(MKUserLocation) {
            return nil
        }
    
        let detailButton: UIButton = UIButton(type: UIButtonType.DetailDisclosure)
    
        if let annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier) {
            annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "pin")
            annotationView.canShowCallout = true
            annotationView.image = UIImage(named: "custom_pin.png")
            annotationView.rightCalloutAccessoryView = detailButton
        }
        else {
            annotationView.annotation = annotation
        }
    
        return annotationView
    }
    

    Regards

    0 讨论(0)
  • 2020-12-07 22:05

    The accepted answer doesn't work, as it has annotationView uninitialized in else block.

    Here's a better solution. It dequeues annotation view if possible or creates a new one if not:

    Swift 3, 4:

    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        // Don't want to show a custom image if the annotation is the user's location.
        guard !(annotation is MKUserLocation) else {
            return nil
        }
    
        // Better to make this class property
        let annotationIdentifier = "AnnotationIdentifier"
    
        var annotationView: MKAnnotationView?
        if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier) {
            annotationView = dequeuedAnnotationView
            annotationView?.annotation = annotation
        }
        else {
            annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
            annotationView?.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
        }
    
        if let annotationView = annotationView {
            // Configure your annotation view here
            annotationView.canShowCallout = true
            annotationView.image = UIImage(named: "yourImage")
        }
    
        return annotationView
    }
    

    Swift 2.2:

    func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
        // Don't want to show a custom image if the annotation is the user's location.
        guard !annotation.isKindOfClass(MKUserLocation) else {
            return nil
        }
    
        let annotationIdentifier = "AnnotationIdentifier"
    
        var annotationView: MKAnnotationView?
        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 {
            // Configure your annotation view here
            annotationView.canShowCallout = true
            annotationView.image = UIImage(named: "yourImage")
        }
    
        return annotationView
    }
    
    0 讨论(0)
  • 2020-12-07 22:06

    SWIFT 3,4

    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    
        if annotation.isKind(of: MKUserLocation.self) {
            return nil
        }
    
        let annotationIdentifier = "AnnotationIdentifier"
    
        var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier)
        if (pinView == nil) {
            pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
        }
    
        pinView?.canShowCallout = false
    
        return pinView
    }
    
    0 讨论(0)
提交回复
热议问题