標記事件和手勢

設定特定的進階標記屬性後,您就可以監控標記事件,例如輕觸和手勢。輕觸標記後,即可查看其他資訊,例如標記標題或文字片段。使用者也可以使用長按手勢移動可拖曳的標記。

回應標記事件

如要回應標記事件,您可以在檢視區塊中加入 GMSMapViewDelegate 通訊協定,並實作對應的回呼。這個範例示範所選標記的 titlesnippet

Swift

// MARK: GMSMapViewDelegate

func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
  if let title = marker.title {
    if let snippet = marker.snippet {
      print("marker title: \(title): snippet: \(snippet)")
    }
  }
  return true
}

Objective-C

// MARK: GMSMapViewDelegate

-   (BOOL)mapView:(GMSMapView *)mapView didTapMarker:(GMSMarker *)marker {
  if (marker.title && marker.snippet) {
    NSLog(@"marker with title:%@ snippet: %@", marker.title,  marker.snippet)
  }
  return YES;
}

依地圖縮放等級控管標記顯示設定

如要控管 GMSMarker 的瀏覽權限,請實作 GMSMapViewDelegate 通訊協定,並新增設定 GMSMarker.map 的條件。

Swift

// MARK: GMSMapViewDelegate

func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) {
    marker.map = position.zoom >= 14 ? mapView : nil
}

Objective-C

// MARK: GMSMapViewDelegate

-   (void)mapView:(GMSMapView *)mapView didChangeCameraPosition:(GMSCameraPosition *)position {
  marker.map = position.zoom >= 14 ? mapView : nil;
}

將標記設計為可拖曳

啟用 draggable 屬性後,使用者就能透過長按手勢在地圖上拖曳標記。如要將標記設計為可拖曳,請將 GMSMarker.draggable 屬性設為 true。

Swift

marker.draggable = true

Objective-C

marker.draggable = YES;