Home IOS Development Working with SwiftUI Maps and Annotations

Working with SwiftUI Maps and Annotations

Working with SwiftUI Maps and Annotations


MapKit is a robust framework that enables builders so as to add maps, annotations, and location-based options to their iOS purposes. With SwiftUI, you’ll be able to simply combine MapKit into your app and create interactive and dynamic maps that supply an awesome person expertise. On this tutorial, we’ll discover find out how to work with maps and annotations in SwiftUI, and find out how to customise the map type and digicam place.

The MapKit Fundamentals

Let’s begin with the fundamentals of MapKit. The MapKit framework features a Map view that builders can use to embed a map in any SwiftUI venture. Right here is an instance:

Earlier than utilizing the Map view, you must import the MapKit framework. Then, to create a map, merely instantiate a Map view. For those who’ve opened the Preview canvas in Xcode, you must see a full display screen map within the simulator.


Altering the Preliminary Place with Map Digicam

As a substitute of displaying a default location, the Map view has one other init technique so that you can change the preliminary place of the map:

You’ll be able to an occasion of MapCameraPosition because the preliminary place of the map. MapCameraPosition comprises varied properties that you need to use to regulate which place or area is displayed, together with:

  • computerized
  • merchandise(MKMapItem) – for displaying a selected map merchandise.
  • area(MKCoordinateRegion) – for displaying a selected area.
  • rect(MKMapRect) – for displaying particular map boundaries.
  • digicam(MapCamera) – for displaying an present digicam place.
  • userLocation() – for displaying the person’s location

As an illustration, you’ll be able to instruct the map to show a selected area by utilizing .area(MKCoordinateRegion):

The coordinates within the above pattern is the GPS coordinates of Occasions Sq. in New York. The worth of span is used to outline your required zoom degree of the map. The smaller the worth, the upper is the zoom degree.


You probably have a specific location for show, you’ll be able to go a map merchandise because the preliminary place. Here’s a pattern code snippet:

Animating the Change of Map Place

The Map view additionally gives an extra init technique that accepts a binding to MapCameraPosition. If you might want to change the place of the map, this init technique is extra acceptable:

For instance, if you wish to add two buttons for customers to modify between two places, you’ll be able to write the code like this:

By wrapping the place variable with withAnimation, the map view will mechanically animate the place change.


This animation works even higher if you present a MapCamera with a pitch angle to create a 3D perspective. To see what occurs, you’ll be able to attempt altering the place of Large Ben within the following line of code:

If you preview the map view, the digicam angle adjusts to indicate a 3D perspective of the area.


Including Markers and Annotations


Markers are a helpful function in MapKit that mean you can show content material at a selected coordinate on the map. It provides an additional layer of knowledge to your map, equivalent to a retailer or a restaurant. Markers may be personalized with a system picture and tint colour, making them visually distinct and simple to acknowledge. Whether or not you’re constructing a navigation app or a journey information, markers are a priceless software that may allow you to create a greater person expertise.

So as to add a marker, you’ll be able to create the Marker view within the map content material builder closure like this:

Optionally, you’ll be able to customise the Marker object with a system picture. To alter the colour of the marker, use the tint modifier:

Along with Marker, SwiftUI now contains an Annotation view in iOS 17 for indicating a location on a map. It capabilities equally to Marker, however presents larger flexibility for personalisation.

So as to add an annotation, you create an Annotation view within the map content material closure. Here’s a pattern code snippet for including a easy annotation:

You could have the flexibleness to customise the annotation in quite a lot of methods. By attaching completely different modifiers to it, you’ll be able to change its look and habits. Moreover, you need to use stack views to rearrange the completely different elements of the annotation and create a format that fits your wants. Right here is an instance:

This outcomes an animated annotation as proven within the under illustration.


Altering the Map Model

By default, the map view renders the map in a regular type. Nonetheless, you’ll be able to change the type by utilizing the mapStyle modifier:

This creates a map type based mostly on satellite tv for pc imagery. By specifying a reasonable elevation, the map view renders a 3D map with a sensible look.


Optionally, you can too change the map type to hybrid like this:


This tutorial covers find out how to work with maps and annotations in SwiftUI, utilizing the MapKit framework. The newest model of SwiftUI presents further APIs and views for builders to additional customise the map view. By now, you must know find out how to embed a map in your app and add an annotation to focus on a location on the map.

If you wish to study extra about SwiftUI, don’t overlook to take a look at our Mastering SwiftUI e-book.



Please enter your comment!
Please enter your name here