Lukas Bimba
Lukas Bimba

Reputation: 826

Swift Mapview Custom Call Out View with default map view pins

I believe this is going to be a really easy answer but I've been trying to figure out how I add a custom callout view with map views default pins. With my current code it seems I can only add an image as the MKPointAnnotation instead of the default pins. This first "viewFor annotation" is how I set up the default pins, while everything underneath is for the custom call out view... What I am trying to do is have my custom call out view with the default pins. Do I have to add a custom image pin if I want a custom call out view?

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

    if let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "") {
        annotationView.annotation = annotation
        return annotationView
    } else {
        let annotationView = MKPinAnnotationView(annotation:annotation, reuseIdentifier:"")
        annotationView.isEnabled = true
        annotationView.canShowCallout = true

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


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

    if annotation is MKUserLocation { return nil }
    var annotationView = self.mapView.dequeueReusableAnnotationView(withIdentifier: "Pin")
    if annotationView == nil{
        annotationView = CustomBusinessCallOutAnnotatiion(annotation: annotation, reuseIdentifier: "Pin")
        annotationView?.canShowCallout = false
    }else{
        annotationView?.annotation = annotation
    }
    annotationView?.image = UIImage(named: "car")
    return annotationView
}

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

    if view.annotation is MKUserLocation { return }

    let customAnnotation = view.annotation as! CustomBusinessPoint
    let views = Bundle.main.loadNibNamed("CustomBusinessCallOut", owner: nil, options: nil)
    let calloutView = views?[0] as! CustomBusinessCallOut


    calloutView.businessName.text = customAnnotation.businessName
    calloutView.businessStreet.text = customAnnotation.businessStreet
    calloutView.businessState.text = customAnnotation.businessState
    calloutView.businessDistance.text = customAnnotation.businessDistance


    calloutView.center = CGPoint(x: view.bounds.size.width / 2, y: -calloutView.bounds.size.height * -0.0001)
    view.addSubview(calloutView)
    mapView.setCenter((view.annotation?.coordinate)!, animated: true)
}

func mapView(_ mapView: MKMapView, didDeselect view: MKAnnotationView) {
    if view.isKind(of: CustomBusinessCallOutAnnotatiion.self) {
        for subview in view.subviews {
            subview.removeFromSuperview()
        }
    }
}

Upvotes: 3

Views: 1995

Answers (1)

Kosuke Ogawa
Kosuke Ogawa

Reputation: 7451

You need not addSubView calloutView. You can use MKAnnotationView as Custom Callout.

e.g. You should arrange the source code

Implement subclass of MKAnnotation and MKAnnotationView.

class PinAnnotation : NSObject, MKAnnotation {
    var coordinate : CLLocationCoordinate2D
    var title: String?
    var calloutAnnotation: CustomBusinessCallOut?

    init(location coord:CLLocationCoordinate2D) {
        self.coordinate = coord
        super.init()
    }
}

class CustomBusinessCallOut : NSObject, MKAnnotation {
    var coordinate: CLLocationCoordinate2D
    var title: String?

    init(location coord:CLLocationCoordinate2D) {
        self.coordinate = coord
        super.init()
    }
}

class CalloutAnnotationView : MKAnnotationView {
}

Implement mapView delegate methods.

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

    if annotation is PinAnnotation {
        let reuseId = "Pin"
        var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId) as? MKPinAnnotationView
        if pinView == nil {
            pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        }
        else {
            pinView?.annotation = annotation
        }

        return pinView
    } else if annotation is CustomBusinessCallOut {
        let reuseId = "Callout"
        var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId)
        if pinView == nil {
            pinView = CalloutAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
            pinView?.addSubview(UIImageView(image: UIImage(named: "car")))
        }
        else {
            pinView?.annotation = annotation
        }

        return pinView
    } else {
        return nil
    }
}

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
    guard view.annotation is PinAnnotation else { return }
    if let pinAnnotation = view.annotation as? PinAnnotation {
        let calloutAnnotation = CustomBusinessCallOut(location: pinAnnotation.coordinate)
        calloutAnnotation.title = pinAnnotation.title
        pinAnnotation.calloutAnnotation = calloutAnnotation
        mapView.addAnnotation(calloutAnnotation)
    }
}

func mapView(_ mapView: MKMapView, didDeselect view: MKAnnotationView) {
    guard view.annotation is PinAnnotation else { return }
    if let pinAnnotation = view.annotation as? PinAnnotation,
        let calloutAnnotation = pinAnnotation.calloutAnnotation {
        mapView.removeAnnotation(calloutAnnotation)
        pinAnnotation.calloutAnnotation = nil
    }
}

Upvotes: 2

Related Questions