Page updated Jan 16, 2024

Work with maps

Amplify iOS v1 is now in Maintenance Mode until May 31st, 2024. This means that we will continue to include updates to ensure compatibility with backend services and security. No new features will be introduced in v1.

Please use the latest version (v2) of Amplify Library for Swift to get started.

If you are currently using v1, follow these instructions to upgrade to v2.

Amplify libraries should be used for all new cloud connected applications. If you are currently using the AWS Mobile SDK for iOS, you can access the documentation here.

Install Amplify-MapLibre adapter

First, ensure you've provisioned an Amazon Location Service Map resource and configured your app using the instructions in either Configure Maps or Use existing resources guide.

Troubleshooting: If your project already imports Amplify through CocoaPods, you may encounter build errors after adding the Amplify-MapLibre adapter. If this occurs, switching to SPM will resolve the issue.

Amplify-MapLibre is an open source adapter that enables the popular MapLibre SDK to work seamlessly with Amplify Geo.

  1. To install the Amplify-MapLibre adapter to your application, open your project in Xcode and select File > Add Packages...

  2. Enter the amplify-ios-maplibre GitHub repo URL (https://github.com/aws-amplify/amplify-ios-maplibre) into the search bar and hit Enter. Wait for the result to load. You'll see the repository rules for which version of amplify-ios-mapLibre you want Swift Package Manager to install.

  3. Choose the dependency rule Up to Next Major Version, as it will use the latest compatible version of the dependency, then click Add Package.

  4. Lastly, choose which of the libraries you want added to your project. If you want to use the SwiftUI user interface components provided by the adapter, select both AmplifyMapLibreAdapter and AmplifyMapLibreUI. If you only want to enable Amplify Geo to work directly with MapLibre and do not wish use use the provided SwiftUI views, you can just select AmplifyMapLibreAdapter. After you select the libraries, click Add Package.

You can always go back and modify which SPM packages are included in your project by opening the Swift Packages tab for your project (Project file > Project > Package Dependencies)

Display a map

First, ensure you've provisioned an Amazon Location Service Map resource and configured your app using the instructions in either Configure maps or Use existing resources guide.

Select your user interface

There are two ways to display a map. The easiest way to get started is to use the AmplifyMaplibreUI library to create an instance of AMLMapView. This is the recommended approach for new projects or anyone who wants to use SwiftUI.

Alternatively, if you are using UIKit or have existing code using the MapLibre/MapBox SDK, you can simply call AmplifyMapLibre.createMap(completionHandler:) to create an instance of MGLMapView that is pre-wired for use with Amazon Location Service and Amplify. For more information on using MGLMapView directly, please see the MapLibre Documentation

1import SwiftUI
2import AmplifyMapLibreUI
3
4struct ContentView: View {
5 var body: some View {
6 AMLMapView()
7 .edgesIgnoringSafeArea(.all)
8 }
9}
1import AmplifyMapLibreAdapter
2import Mapbox
3import Amplify
4
5var mapView: MGLMapView?
6
7AmplifyMapLibre.createMap { result in
8 switch result {
9 case .failure(let error):
10 print(error)
11 case .success(let map):
12 mapView = map
13 }
14}

Customize the map and access state

The AMLMapView can be customized through a number of view modifiers. The map state information can be set and observed through an instance of AMLMapViewState, which can optionally be passed into AMLMapView to set initial values. The following example sets an initial zoom level and center location for the map and configures the map to show the user's location.

1@StateObject private var mapState = AMLMapViewState(
2 zoomLevel: 8,
3 center: CLLocationCoordinate2D(latitude: 39.7392, longitude: -104.9903)
4)
5
6var body: some View {
7 AMLMapView(mapState: mapState)
8 .showUserLocation(true)
9 .edgesIgnoringSafeArea(.all)
10}

Inject custom behavior

The AMLMapView also allows for custom behavior triggered by user interaction to be injected. The following example sets a custom feature image and defines the maps behavior when that feature is tapped - zooming in two levels above the current level.

1var body: some View {
2 AMLMapView()
3 .featureImage { MyCustomImage() }
4 .featureTapped { mapView, pointFeature in
5 mapView.setCenter(
6 pointFeature.coordinate,
7 zoomLevel: mapView.zoomLevel + 2,
8 direction: mapView.camera.heading,
9 animated: true
10 )
11 }
12 .edgesIgnoringSafeArea(.all)
13}

AMLMapCompositeView

The AMLMapCompositeView combines AMLMapView, AMLSearchBar, AMLMapControlView, and AMLPlaceCellView to create a full user experience. This includes accessible map control buttons, a search bar that automatically searches for points of interest based on user input, and a list representation of points. In its simplest form, which still leverages all of the above mentioned functionality, the AMLMapCompositeView can be instantiated without any arguments. All of the view modifiers and state tracking capabilities of AMLMapView are also available on AMLMapCompositeView.

1var body: some View {
2 AMLMapCompositeView()
3}

For full details on AMLMapView usage and customization, see the AmplifyMapLibre documentation.

Display different map styles

The availableMaps API fetches information for all maps that are available to be displayed.

This is useful if you would like to give your users a variety of maps styles to choose from.

1var maps = [Geo.MapStyle]()
2
3Amplify.Geo.availableMaps { result in
4 switch result {
5 case .failure(let error):
6 print(error)
7 case .success(let availableMaps):
8 maps = availableMaps
9 }
10}

You can load a different style map by passing it to the createMap function.

1guard let mapStyle = maps.first else {
2 print("No maps available")
3 return
4}
5
6let mapView = AmplifyMapLibre.createMap(mapStyle)