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

letcamera=GMSCameraPosition(target:coordinate,zoom:14)
letmapID=GMSMapID(identifier:"YOUR_MAP_ID")
letmapView=GMSMapView(frame:view.bounds,mapID:mapID,camera:camera)
letmarker=GMSAdvancedMarker(position:coordinate)
marker.map=mapView

Objective-C

GMSCameraPosition*camera=[GMSCameraPositioncameraWithTarget:kCoordinatezoom:16];
GMSMapID*mapID=[GMSMapIDmapIDWithIdentifier:"YOUR_MAP_ID"];
self.mapView=[GMSMapViewmapWithFrame:self.view.boundsmapID:mapIDcamera:camera];
GMSAdvancedMarker*marker=[GMSAdvancedMarkermarkerWithPosition: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

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

Objective-C

GMSCameraPosition*camera=[GMSCameraPositioncameraWithLatitude:-33.8683
longitude:151.2086
zoom:6];
mapView=[GMSMapViewmapWithFrame:CGRectZerocamera:camera];
// ...
[mapViewclear];

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

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

Objective-C

CLLocationCoordinate2Dposition=CLLocationCoordinate2DMake(10,10);
GMSAdvancedMarker*marker=[GMSAdvancedMarkermarkerWithPosition: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

// ...
letadvancedMarker:GMSAdvancedMarker={
GMSAdvancedMarker(position:CLLocationCoordinate2D(latitude:47.6089945,longitude:-122.3410462))
}()
letmarker:GMSMarker={
GMSMarker(position:CLLocationCoordinate2D(latitude:47.6089945,longitude:-122.3410462))
}()
funcaddMarker(){
ifmapView.mapCapabilities.contains(.advancedMarkers){
advancedMarker.map=mapView
}else{
marker.map=mapView
}
}
extensionMapCapabilities:GMSMapViewDelegate{
funcmapView(_mapView:GMSMapView,didChangeMapCapabilitiesmapCapabilities:GMSMapCapabilityFlags){
letadvancedMarkerAvailable=mapCapabilities.contains(.advancedMarkers)
advancedMarker.map=advancedMarkerAvailable?mapView:nil
marker.map=advancedMarkerAvailable?nil:mapView
}
}

Objective-C

// ...
_advancedMarker=[GMSAdvancedMarkermarkerWithPosition:kSeattleCoordinates];
_fallbackMarker=[GMSMarkermarkerWithPosition:kSeattleCoordinates];
- (void)addMarker{
if(_mapView.mapCapabilities&GMSMapCapabilityFlagsAdvancedMarkers){
_advancedMarker.map=_mapView;
}else{
_fallbackMarker.map=_mapView;
}
}
#pragma mark - GMSMapViewDelegate
- (void)mapView:(GMSMapView*)mapView
didChangeMapCapabilities:(GMSMapCapabilityFlags)mapCapabilities{
BOOLadvancedMarkersAvailable=mapCapabilities&GMSMapCapabilityFlagsAdvancedMarkers;
_advancedMarker.map=advancedMarkersAvailable?_mapView:nil;
_fallbackMarker.map=advancedMarkersAvailable?nil:_mapView;
}

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025年11月21日 UTC.