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 lets 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 does 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 doesn't 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

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

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

    6. When initializing your map view, set configuration options with GMSMapViewOptions. Properties include the frame, camera, mapID,backgroundColor or screen.

    7. Set your map options camera property with a GMSCameraPosition object. This specifies the center and zoom level of the map.

    8. Create and instantiate a GMSMapView class using the GMSMapView options: method. If this map is to be used as the view controller's only view, the map option frame default value of CGRectZero can be used as the view frame — the map is resized automatically.

    9. Set the GMSMapView object as the view controller's view. For example, 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 options = GMSMapViewOptions()
    options.camera = GMSCameraPosition(latitude: 1.285, longitude: 103.848, zoom: 12)
    options.frame = self.view.bounds;

    let mapView = GMSMapView(options:options)
    self.view = mapView
  }
}

Objective-C

- (void)viewDidLoad {
  [super viewDidLoad];

  GMSMapViewOptions *options = [[GMSMapViewOptions alloc] init];
  options.camera = [GMSCameraPosition cameraWithLatitude:1.285
                                                        longitude:103.848
                                                             zoom:12];
  options.frame = self.view.bounds;

  GMSMapView *mapView = [[GMSMapView alloc] initWithOptions:options];
  self.view = mapView;
}

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.