At WWDC 2023, Apple announced improved SwiftUI support for MapKit. While a fully native SwiftUI version may take time due to MapKit’s huge API, displaying an interactive map view in your app is becoming easier. Annotations, the most widely-used feature, has received a major improvement, and the new map style modifier requires minimal effort to add a visually appealing touch to your app. MapKit is gradually adopting a more SwiftUI-compatible interface, but rewriting of some Map code may be necessary as Xcode 15 replaces all Xcode 12 Map initializers. Download the starter project by clicking ‘Download materials’ located at the top or bottom of the article and open it in Xcode 15 beta to begin.
This article uses the starter project called ‘PublicArt’, which was first created in 2014 for a raywenderlich.com tutorial on MapKit. The original tutorial was later updated by Andrew Tetlaw in 2020, and in 2019, the author adapted ‘PublicArt’ for use in the ‘SwiftUI Tutorial: Navigation.’ Previously in Xcode 11, a struct named ‘MapView: UIViewRepresentable’ was created to display an MKMapView in a SwiftUI app. Most recently, Josh Steele updated the project to Xcode 14.2 for the SwiftUI Fundamentals course. The starter project provided for this article uses the SwiftUI Map that debuted in Xcode 12 and is set to iOS Deployment 17.
Following the deprecation of all Xcode 12 map initializers in this year’s Xcode 15, a Map instance is now created using MapCameraBounds or MapCameraPosition rather than bindings to MKCoordinateRegion or MKMapRect. The MapMarker is replaced by the Marker, which supports specifying a tint color, whereas, the newly added Annotation structure displays both a label View and a content View, allowing the customization of map pins.
Perhaps the best new feature in MapKit for SwiftUI is the mapStyle modifier, which adds a ton of visual pizzazz to your app. The ‘onMapCameraChange(frequency:)’ method of MapPitchButton and the MapPolyline and MKRoute can be used for various nifty features. Additionally, the MapCompass and MapScaleView have been added to the mix of functionalities. After running the starter project, follow the article to learn how to utilize these new features and create visually compelling interactive maps in your app.
Source link