At WWDC 2023, Apple introduced higher SwiftUI assist for MapKit. MapKit is a big API, so it’ll be some time earlier than we see a completely native SwiftUI model. Nevertheless it’s positively getting simpler to show an interactive map view in your app. Essentially the most widely-used function — annotations — will get a giant enchancment, and the brand new map type modifier requires nearly no work so as to add a ton of pizzazz to your app. Slowly however certainly, MapKit is buying a extra SwiftUI “contact and really feel”.
On the draw back, for those who’ve written a whole lot of Map
code prior to now couple of years, you would possibly must rewrite a few of it, as Xcode 15 replaces all of the Xcode 12 Map
initializers.
Getting Began
Click on Obtain supplies on the prime or backside of this text to obtain the starter mission. Open it in Xcode 15 beta to see what it’s important to work with.
PublicArt
I wrote the primary model of PublicArt in 2014 for my very first raywenderlich.com tutorial MapKit Tutorial: Getting Began. It was an replace of Ray’s unique MapKit tutorial, and Andrew Tetlaw up to date it once more in 2020.
In 2019, I tailored PublicArt for SwiftUI Tutorial: Navigation. Utilizing Xcode 11, I needed to create a struct MapView: UIViewRepresentable
to show an MKMapView
in a SwiftUI app.
Earlier this 12 months, Josh Steele up to date PublicArt to Xcode 14.2 for our SwiftUI Fundamentals course. That is the starter mission for this text: It makes use of the SwiftUI Map
launched in Xcode 12.
For this text, the starter mission has iOS Deployment set to iOS 17.
Xcode 12 Map
Open LocationMap within the code editor. There’s an MKCoordinateRegion @State
property. You go a binding to this into the Map
initializer, together with an array of annotationItems
, then show a MapMarker
utilizing an paintings
merchandise’s coordinate
property. When the Map
view seems, you set area.middle
and area.span
:
@State var area = MKCoordinateRegion()
...
Map(coordinateRegion: $area, annotationItems: [artwork]) { paintings in
MapMarker(coordinate: paintings.coordinate)
}
.onAppear {
area.middle = paintings.coordinate
area.span = MKCoordinateSpan(latitudeDelta: 0.02, longitudeDelta: 0.02)
}
Refresh the preview or construct and run the app, and navigate to the map:
Xcode 15 Beta Map
This 12 months’s Map
deprecates all of the Xcode 12 initializers. As a substitute of bindings to MKCoordinateRegion
or MKMapRect
, you create a Map
with MapCameraBounds
or MapCameraPosition
.
You may create a MapCameraBounds
worth with an MKCoordinateRegion
or MKMapRect
worth, plus minimal and most distances, or you may create one utilizing solely minimal and most distances.
Or you may create a Map
utilizing no parameters in any respect!
Map & Marker
In LocationMap, remark out @State var area
and substitute all of the Map
code — Map(...) { ... }.onAppear { ... }
— with this:
Map {
Marker(paintings.title, coordinate: paintings.coordinate)
}
MapMarker
is deprecated, changed by Marker
, the place you show a label, which is a View
.
You get the identical balloon marker, however the map has zoomed proper in to it. To point out roughly the identical area because the previous area
, initialize Map
with bounds
:
Map(bounds:
MapCameraBounds(minimumDistance: 4500,
maximumDistance: 4500))
Annotation
Along with Marker
, there’s a brand new Annotation
construction:
Annotation(paintings.title,
coordinate: paintings.coordinate) {
Picture(systemName: "particular person.bust")
.padding(6)
.foregroundStyle(.white)
.background(Shade.blue)
}
An Annotation
shows each a label View
and a content material View
, so you may customise your map pins. For instance, add these properties to Art work:
var image: String {
change self-discipline {
case "Monument", "Sculpture":
return "particular person.bust"
case "Mural":
return "paintpalette"
case "Plaque":
return "particular person.textual content.rectangle"
default:
return "mappin"
}
}
var background: Shade {
change self-discipline {
case "Monument", "Sculpture":
return .blue
case "Mural":
return .mint
case "Plaque":
return .orange
default:
return .pink
}
}
Every paintings has a self-discipline
property, and these new properties specify symbols and background colours for the most-populated disciplines.
Now, substitute your Marker
and Annotation
code with:
Marker(paintings.title, systemImage: paintings.image,
coordinate: paintings.coordinate)
.tint(paintings.background)
Annotation(paintings.title,
coordinate: paintings.coordinate,
anchor: .topLeading) {
Picture(systemName: paintings.image)
.padding(6)
.foregroundStyle(.white)
.background(paintings.background)
}
Just like the deprecated MapMarker
, Marker
helps you to specify tint
.
To see these customized symbols and colours at work, change the index of the artData
merchandise within the preview to 1. This paintings is a mural, so that you get mint-colored pins, and the annotation image is a paint palette:
You most likely wouldn’t need to use each Marker
and Annotation
for a similar location, however whereas they’re each there, see how one can modify the place of the annotation’s anchor
:
Annotation(paintings.title,
coordinate: paintings.coordinate,
anchor: .topLeading) // add this argument
The worth of anchor
will be prime, backside, main, trailing or combos, or you may specify a CGPoint
with x and y coordinates.
Map Fashion
And now for probably the greatest new options: mapStyle
. Add this modifier to Map
after its closing brace:
.mapStyle(.imagery(elevation: .sensible))
And alter bounds
to zoom in:
Map(bounds:
MapCameraBounds(minimumDistance: 1500,
maximumDistance: 1500))
Shift-Choice-drag to maneuver the map digital camera angle:
It’s also possible to add an initialPosition
parameter to Map
to set the map digital camera:
Map(
initialPosition: .digital camera(MapCamera(
centerCoordinate: paintings.coordinate,
distance: 1200,
heading: 90,
pitch: 60)),
bounds:
MapCameraBounds(minimumDistance: 1500,
maximumDistance: 1500)) {
Now, you may take an aerial tour round Honolulu by altering the artData
merchandise within the preview. For instance, artData[12]
:
There are solely 17 artworks, so don’t transcend artData[16]
.
The place to Go From Right here?
Obtain the ultimate mission utilizing Obtain supplies on the prime or backside of this text.
On this article, you discovered about:
- The brand new approach to create a
Map
. - The brand new
Marker
andAnnotation
constructions. - The brand new
MapStyle
construction. - The brand new
MapCamera
construction.
Meet MapKit for SwiftUI exhibits off a number of nifty options, together with:
-
onMapCameraChange(frequency:)
, an occasion methodology ofMapPitchButton
. -
MapPolyline
andMKRoute
. -
MapCompass
andMapScaleView
.
We hope you loved this tutorial, and when you have any questions or feedback, please be part of the discussion board dialogue under!