MapKit
makes it possible to show maps at a particular location. The user can change the location by swiping around, and they can zoom in and out with a pinch gesture. When creating the map, we provide a location with a latitude and longitude coordinate. The zoom level is determined by a delta in both latitude and longitude which seems to be between zero and 180.
Before you do anything, you will need to add the Maps capability to your app project.
Now you can start by creating ContentView with the data we need. The default location I have here is that of Apple’s headquarters, and the zoom level should make it fairly clear where the map is located.
I’ve used computed properties to combine the latitude and longitude for both the location and the zoom level, and I have a function that sets the current region.
Now I’m going to add conformance to the View
protocol. First I’m adding controls
, which has a Slider
for the location latitude and longitude, as well as the zoom latitude and longitude deltas.
Then I simply combine these in a VStack
with the Map
, which takes the region
as its coordinateRegion
.
I am using an old MacBook Air at the moment, and it could not handle live updating the location.
That’s why I added a Button
for updating the location, but you may find it’s possible to call setLocation
in the onEditingChanged
parameter of each Slider
.