SwiftUI MapKit Callouts

I think they are called callouts.
I want to create custom map annotations using swiftUI Views in the map this is the code i'm using:

Code Block Swift
Map(coordinateRegion: $coordinateStart, interactionModes: interactionMode, showsUserLocation: true, userTrackingMode: $trackingMode, annotationItems: locations, annotationContent:{(location)in
                        MapAnnotation(coordinate: location.coordinate){ EventMapAnnotation()}
                    })


The custom map Annotation uses the view "EventMapAnnotation" to show a yellow circle that I'm using to test as a button. Now I want this button or this Annotation to show me a window or a view with more details about the location.

I've heard of something called a "Callout" But im not sure if the pure SwiftUI map supports it yet.

Replies

Hi,
sadly the SwiftUI MapKit implementation is very poor at the moment. You can wrap the EventMapAnnotation in a Button(action: {}), this at least gives you the option of calling a function that will then trigger some action.
Take care,
David
You can create your own custom AnnotationViews using the MapAnnotation. See below an example that creates a callout with an image.

Code Block
Map(coordinateRegion: $coordinateRegion, annotationItems: annotations) { annotation in
   MapAnnotation(coordinate: annotation.coordinate, anchorPoint: CGPoint(x: 0.5, y: 1)) {
       AssetAnnotationView(image: annotation.image)
   }
}

Where AssetAnnotationView could look like this:
Code Block
struct AssetAnnotationView: View {
    var image: UIImage
    var body: some View {
        Image(uiImage: image)
            .resizable()
            .aspectRatio(contentMode: .fill)
            .frame(width: 100, height: 100)
            .cornerRadius(3.0)
            .padding(4)
            .callOut()
        }
    }
}

callout is a custom modifier here that draws the callout shape (a rounded rect with a beak/point to the bottom of the view)

  • It's sad that there is no easy way to do this. Even for just a simple title/subtitle. It should have been integrated into MapMarker and MapPin

Add a Comment