创建高级标记

请选择平台: Android iOS JavaScript

使用 GMSAdvancedMarker 子类创建基本或 具体标记地图项,如下所示。作为 GMSMarkerGMSAdvancedMarker 为标记提供了 表达式。

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 之前,特定地图功能可用。通过 didChangeMapCapabilities 函数 GMSMapViewDelegate也是 在功能发生变化时调用。 此查询用于确定地图视图是否支持高级标记。

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