创建高级标记

请选择平台: Android iOS JavaScript

使用 GMSAdvancedMarker 子类创建基本或特定标记地图项,如下所示。作为 GMSMarker 的子类,GMSAdvancedMarker 可为标记提供更多表达方式。

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;

移除高级标记

GMSMarker 类似,您可以通过将 GMSAdvancedMarker 的地图属性设置为 nil,从地图中移除高级标记。或者,您也可以通过调用 GMSMapView clear 方法来移除地图上的所有叠加层(包括高级标记)。

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];
      

如果您在将标记添加到地图后想要对其进行修改,请务必保留 GMSAdvancedMarker 对象。日后您可以通过更改此对象来修改标记。

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;

地图功能

GMSMapView 上的 mapCapabilities 属性会为特定于地图的功能添加程序化检查。如果您想在调用特定 API 之前了解某些地图功能是否可用,此方法非常有用。当功能发生变化时,系统还会调用 GMSMapViewDelegatedidChangeMapCapabilities 函数。此查询用于确定地图视图是否支持高级标记。

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;
}