0

I found this article and I'm trying to add functionality to Map.

I have created this extension and I want to show an overlay to the map. Currently it's not being shown and I think it is because I have not specified a MKPolylineRenderer

How can I specify a renderer in this extension?

extension Map {
    func mapStyle(_ mapType: MKMapType, showScale: Bool = true, showTraffic: Bool = false) -> some View {
        let map = MKMapView.appearance()
        map.mapType = mapType
        map.showsScale = showScale
        map.showsTraffic = showTraffic
        return self
    }

    func addAnnotations(_ annotations: [MKAnnotation]) -> some View {
        MKMapView.appearance().addAnnotations(annotations)
        return self
    }
    
    func addOverlay(_ overlay: MKOverlay) -> some View {
        MKMapView.appearance().addOverlay(overlay)
        return self
    }
    
}

View

Map(coordinateRegion: .constant(MKCoordinateRegion(route.polyline.boundingMapRect)))
.addOverlay(route.polyline)

I know I can use UIViewRepresentable and I was using that but the issue is that it does not respond well to touching and moving around. It gets stuck and scrolling is hard. With Map it works perfectly.

M1X
  • 4,971
  • 10
  • 61
  • 123

1 Answers1

0

I think using UIViewRepresentable it's the only working solution for now, because SwiftUI Map does not support yet Overlays and other feature that you can use in UIKit Maps.

How you can read on documentation (https://developer.apple.com/documentation/mapkit/map) a SwiftUI Map just displays a Region and annotations at specific locations.

I think there will be updates in the future, for now I think this solution is quit acceptable : SwiftUI Map Overlays without UIViewRepresentable

AntonioWar
  • 294
  • 1
  • 6