How to add compass or rotation to MapKit Map with SwiftUI

386 Views Asked by At

I need to add Compass or rotation with Map. This is my code. I am using Custom marker. I know we can add compass with MKMapView. But I have added many features to the Map using Map class like custom View Markers, longPress pin drop etc. Therefore, moving to MKMapView would be like redoing everything from scratch again.

Map(coordinateRegion: $mapRegion , annotationItems: viewModel.locations) { location in
            MapAnnotation(coordinate: location.coordinate) {
                MarkerView(location: location) 
            }
        }

Anybody can tell me how I can do compass rotation with Map.

Thanks

1

There are 1 best solutions below

1
Alex Fringes On

You may be able to set these properties on the underlying view via the Introspect framework. It does not handle Map() out of the box, so you would have to follow the section on implementing your own selector. As the project's Read Me states, there are some caveats:

Please note that this introspection method might break in future SwiftUI releases. Future implementations might not use the same hierarchy, or might not use UIKit elements that are being looked for. Though the library is unlikely to crash, the .introspect() method will not be called in those cases.

I have also found that you need to really be careful and heavily test which property you set (or update) when and where.