Search code examples
iosswiftannotationsmapboxcallout

Mapbox Callout Accessory


I was wondering if Mapbox had any other callout buttons rather than just the seven provided?

let button = UIButton(type: .detailDisclosure)

Or if there was a quick fix to converting these UIButtons into UIImages in order to create my own buttons.


Solution

  • Yes, you can use the methods func mapView(_ mapView: MGLMapView, leftCalloutAccessoryViewFor annotation: MGLAnnotation) -> UIView? and func mapView(_ mapView: MGLMapView, rightCalloutAccessoryViewFor annotation: MGLAnnotation) -> UIView? to create your own. Set the button type to .custom

    For example:

    func mapView(_ mapView: MGLMapView, leftCalloutAccessoryViewFor annotation: MGLAnnotation) -> UIView? {
        // We're not concerned with the user annotation.
        guard annotation is CameraAnnotation || annotation is NoteAnnotation else {
            return nil
        }
        // Callout height is fixed; width expands to fit its content.
        let deleteButton = UIButton(type: .custom)
        deleteButton.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
        deleteButton.setImage(UIImage(named: "delete"), for: .normal)
        deleteButton.tag = 100
        return deleteButton
    }