Create an advanced marker

  • The GMSAdvancedMarker subclass in the Maps SDK for iOS allows you to create markers with enhanced features and customization options, inheriting functionalities from GMSMarker.

  • You can remove an advanced marker by setting its map property to nil or use the clear method of GMSMapView to remove all overlays, including advanced markers.

  • To modify an existing marker, retain the GMSAdvancedMarker object and make changes to its properties.

  • The mapCapabilities property of GMSMapView enables checking for map features programmatically, such as support for advanced markers, before using related APIs, ensuring compatibility.

  • The didChangeMapCapabilities function of the GMSMapViewDelegate is triggered when map capabilities change, offering a way to dynamically adapt marker usage based on available features.

Select platform: Android iOS JavaScript

Use the GMSAdvancedMarker subclass to create basic or specific marker features, as shown below. As a subclass of GMSMarker, GMSAdvancedMarker provides markers with more expression.

Swift

let camera = GMSCameraPosition( target: coordinate, zoom: 14)
let mapID = GMSMapID(identifier: "YOUR_MAP_ID")
let mapView = GMSMapView(frame: view.bounds, mapID: mapID, camera: camera)

let marker = GMSAdvancedMarker(position: coordinate)
marker.map = mapView

Objective-C

GMSCameraPosition *camera = [GMSCameraPosition cameraWithTarget:kCoordinate zoom:16];
GMSMapID *mapID = [GMSMapID mapIDWithIdentifier:"YOUR_MAP_ID"];

self.mapView = [GMSMapView mapWithFrame:self.view.bounds mapID:mapID camera:camera];

GMSAdvancedMarker *marker = [GMSAdvancedMarker markerWithPosition:kCoordinate];
Marker.map = self.mapView;

Remove an advanced marker

Similar to GMSMarker, you can remove an advanced marker from the map by setting the map property of the GMSAdvancedMarker to nil. Alternatively, you can remove all of the overlays (including advanced markers) on the map by calling the GMSMapView clear method.

Swift

let camera = GMSCameraPosition.camera(
  withLatitude: -33.8683,
  longitude: 151.2086,
  zoom: 6
)
let mapView = GMSMapView.map(withFrame: .zero, camera: camera)
// ...
mapView.clear()
      

Objective-C

GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.8683
                                                        longitude:151.2086
                                                             zoom:6];
mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
// ...
[mapView clear];
      

If you want to make modifications to a marker after you've added it to the map, ensure that you keep hold of the GMSAdvancedMarker object. You can modify the marker later by making changes to this object.

Swift

let position = CLLocationCoordinate2D(latitude: 10, longitude: 10)
let marker = GMSAdvancedMarker(position: position)
marker.map = mapView
// ...
marker.map = nil

Objective-C

CLLocationCoordinate2D position = CLLocationCoordinate2DMake(10, 10);
GMSAdvancedMarker *marker = [GMSAdvancedMarker markerWithPosition:position];
marker.map = mapView;
// ...
marker.map = nil;

Map capabilities

The mapCapabilities property on GMSMapView adds programmatic checking for map-specific features. This is useful when wanting to know if certain map capabilities are available before calling specific APIs. The didChangeMapCapabilities function of GMSMapViewDelegate is also invoked as capabilities change. This query determines if the map view supports advanced markers.

Swift

// ...

let advancedMarker: GMSAdvancedMarker = {
GMSAdvancedMarker(position: CLLocationCoordinate2D(latitude: 47.6089945, longitude: -122.3410462))
}()

let marker: GMSMarker = {
GMSMarker(position: CLLocationCoordinate2D(latitude: 47.6089945, longitude: -122.3410462))
}()

func addMarker() {
  if mapView.mapCapabilities.contains(.advancedMarkers) {
      advancedMarker.map = mapView
    } else {
      marker.map = mapView
    }
}

extension MapCapabilities: GMSMapViewDelegate {
  func mapView(_ mapView: GMSMapView, didChangeMapCapabilities mapCapabilities: GMSMapCapabilityFlags) {

     let advancedMarkerAvailable = mapCapabilities.contains(.advancedMarkers)

     advancedMarker.map = advancedMarkerAvailable ? mapView : nil
     marker.map = advancedMarkerAvailable ? nil : mapView
  }
}

Objective-C

// ...

_advancedMarker = [GMSAdvancedMarker markerWithPosition:  kSeattleCoordinates];
_fallbackMarker = [GMSMarker markerWithPosition: kSeattleCoordinates];

-   (void)addMarker {

  if (_mapView.mapCapabilities & GMSMapCapabilityFlagsAdvancedMarkers) {
    _advancedMarker.map = _mapView;
    } else {
    _fallbackMarker.map = _mapView;
  }
}

#pragma mark - GMSMapViewDelegate

-   (void)mapView:(GMSMapView *)mapView
    didChangeMapCapabilities:(GMSMapCapabilityFlags)mapCapabilities {
  BOOL advancedMarkersAvailable = mapCapabilities & GMSMapCapabilityFlagsAdvancedMarkers;
  _advancedMarker.map = advancedMarkersAvailable ? _mapView : nil;
  _fallbackMarker.map = advancedMarkersAvailable ? nil : _mapView;
}