Add a map

Select platform: Android iOS JavaScript

Maps are represented in the API by the GMSMapView class, a subclass of UIView. The map is the most significant object in the Maps SDK for iOS, and provides necessary methods for adding, removing and managing other objects such as markers and polylines.

Introduction

The Maps SDK for iOS allows you to display a Google map in your iOS application. These maps have the same appearance as the maps you see in the Google Maps iOS app, and the SDK exposes many of the same features.

In addition to mapping functionality, the API also supports a range of interactions that are consistent with the iOS UI model. For example, you can set up interactions with a map by defining responders that react to user gestures, such as tap and double-tap.

The key class when working with a Map object is the GMSMapView class. GMSMapView handles the following operations automatically:

  • Connecting to the Google Maps service.
  • Downloading map tiles.
  • Displaying tiles on the device screen.
  • Displaying various controls such as pan and zoom.
  • Responding to pan and zoom gestures by moving the map and zooming in or out.
  • Responding to two finger gestures by tilting the viewing angle of the map.

In addition to these automatic operations, you can control the behavior and appearance of the map through the properties and methods exposed by the GMSMapView class. GMSMapView allows you to add and remove markers, ground overlays and polylines, change the type of map that is displayed, and control what is shown on the map through the GMSCameraPosition class.

Build Maps with SwiftUI

SwiftUI offers an additional way to create UI using a declarative approach. You tell SwiftUI how you want your view to look along with all the different states for it, and the system will do the rest. SwiftUI handles updating the view whenever the underlying state changes due to an event or user action.

Maps SDK for iOS is built on top of UIKit and does not yet provide a SwiftUI-compatible view. Adding maps in SwiftUI requires conforming to either UIViewRepresentable or UIViewControllerRepresentable. To learn more, see the Codelab adding a map to your iOS app with SwiftUI.

Adding a map

The basic steps for adding a map are:

  1. To get the SDK, obtain an API key, and add the required frameworks, follow the steps in:

    1. Set Up in the Google Cloud Console

    2. Use an API key

    3. Set up an Xcode Project

  2. In your AppDelegate, provide your API key to the provideAPIKey: class method on GMSServices.

  3. Create or update a ViewController. If the map will be displayed when this view controller becomes visible, be sure to create it within the loadView method.

    1. Create a GMSCameraPosition object that specifies the center and zoom level of the map. When you instantiate the GMSMapView object, you must pass the GMSCameraPosition object as a required parameter.

    2. Create and instantiate a GMSMapView class using the GMSMapView mapWithFrame: method. If this map is to be used as the view controller's only view, then CGRectZero could be used as the map's frame — the map will be resized automatically.

    3. Set the GMSMapView object as the view controller's view, e.g. self.view = mapView;.

The below example adds a map, centered at downtown Singapore, to an app.

Swift

import GoogleMaps

class MapObjects : UIViewController {
  override func viewDidLoad() {
    super.viewDidLoad()
    let camera = GMSCameraPosition(latitude: 1.285, longitude: 103.848, zoom: 12)
    let mapView = GMSMapView(frame: .zero, camera: camera)
    self.view = mapView
  }
}
      

Objective-C

#import "MapObjects.h"
@import GoogleMaps;

@implementation MapObjects

- (void)viewDidLoad {
  [super viewDidLoad];
  GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:1.285
                                                          longitude:103.848
                                                               zoom:12];
  GMSMapView *mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
  self.view = mapView;
}

@end
      

Once you've followed these steps, you may further configure the GMSMapView object.

What's next

After you complete these steps, you can configure the map settings.