Events

Select platform: Android iOS JavaScript

Using the Maps SDK for iOS, you can listen to events that occur on the map, such as camera change events or marker tap events.

Introduction

To listen to events, you must implement the GMSMapViewDelegate protocol. Typically, you implement this protocol on the view controller that displays the map. Below is an example:

Swift

import GoogleMaps

class Events: UIViewController, GMSMapViewDelegate {
  // ...
}
      

Objective-C

@import GoogleMaps;

@interface Events : UIViewController <GMSMapViewDelegate>

@end
      

When the GMSMapView is created, you can set its delegate to your view controller. The GMSMapViewDelegate provides only optional methods. To listen to any particular event, you must implement the relevant method.

Swift

override func loadView() {
  super.loadView()
  let camera = GMSCameraPosition.camera(
    withLatitude: 1.285,
    longitude: 103.848,
    zoom: 12
  )
  let mapView = GMSMapView.map(withFrame: .zero, camera: camera)
  mapView.delegate = self
  self.view = mapView
}

// MARK: GMSMapViewDelegate

func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) {
  print("You tapped at \(coordinate.latitude), \(coordinate.longitude)")
}
      

Objective-C

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

#pragma mark - GMSMapViewDelegate

- (void)mapView:(GMSMapView *)mapView didTapAtCoordinate:(CLLocationCoordinate2D)coordinate {
  NSLog(@"You tapped at %f,%f", coordinate.latitude, coordinate.longitude);
}
      

Camera position

Using the GMSMapViewDelegate, you can listen to changes to the camera position used to render the map. There are three distinct events.

  • mapView:willMove: indicates that the camera position is about to change. If the gesture argument is set to YES, this is due to a user performing a natural gesture on the GMSMapView, such as a pan or tilt. Otherwise, NO indicates that this is part of a programmatic change - for example, via methods such as animateToCameraPosition: or updating the map's layer directly. This may also be NO if a user has tapped on the My Location or compass buttons, which generate animations that change the camera.

    This method may be called several times before mapView:idleAtCameraPosition: is invoked, although this typically happens only if animations and gestures occur at the same time - a gesture will cancel any current animation, for instance, and will call mapView:willMove: a second time.

  • mapView:didChangeCameraPosition: is called repeatedly during a gesture or animation, always after a call to mapView:willMove:. It is passed the intermediate camera position.

  • Finally, mapView:idleAtCameraPosition: is invoked once the camera position on GMSMapView becomes idle, and specifies the relevant camera position. At this point, all animations and gestures have stopped.

    Applications can use this event to trigger a refresh of markers or other content being displayed on the GMSMapView, rather than, for example, reloading the content on every camera change.

For example, an application can clear the GMSMapView on move, and then reverse geocode the position the camera comes to rest on.

Swift

let geocoder = GMSGeocoder()

func mapView(_ mapView: GMSMapView, willMove gesture: Bool) {
  mapView.clear()
}

func mapView(_ mapView: GMSMapView, idleAt cameraPosition: GMSCameraPosition) {
    geocoder.reverseGeocodeCoordinate(cameraPosition.target) { (response, error) in
      guard error == nil else {
        return
      }

      if let result = response?.firstResult() {
        let marker = GMSMarker()
        marker.position = cameraPosition.target
        marker.title = result.lines?[0]
        marker.snippet = result.lines?[1]
        marker.map = mapView
      }
    }
  }
      

Objective-C

GMSGeocoder *geocoder;

- (void)mapView:(GMSMapView *)mapView willMove:(BOOL)gesture {
  [mapView clear];
}

- (void)mapView:(GMSMapView *)mapView idleAtCameraPosition:(GMSCameraPosition *)cameraPosition {
  id handler = ^(GMSReverseGeocodeResponse *response, NSError *error) {
    if (error != nil) {
      return;
    }
    GMSReverseGeocodeResult *result = response.firstResult;
    GMSMarker *marker = [GMSMarker markerWithPosition:cameraPosition.target];
    marker.title = result.lines[0];
    marker.snippet = result.lines[1];
    marker.map = mapView;
  };
  [geocoder reverseGeocodeCoordinate:cameraPosition.target completionHandler:handler];
}
      

Events on businesses and other points of interest

By default, points of interest (POIs) appear on the base map along with their corresponding icons. POIs include parks, schools, government buildings, and more, as well as business POIs such as shops, restaurants, and hotels.

You can respond to click events on a POI. See the guide to businesses and other points of interest.

Other events

To learn about the full list of methods on GMSMapViewDelegate, see the reference guide.