建立進階標記

選取平台: Android iOS JavaScript

使用 GMSAdvancedMarker 子類別建立基本或特定標記地圖項目,如下所示。GMSAdvancedMarkerGMSMarker 的子類別,可提供更多表達方式的標記。

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 物件。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;

地圖功能

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