設定特定的進階標記屬性後,您就可以監控輕觸和手勢等標記事件。輕觸標記即可查看其他資訊,例如標記標題或文字片段。此外,使用長按手勢即可移動可拖曳的標記。
- 如要追蹤標記事件,請將
GMSMapViewDelegate
新增至view
。 - 如要將標記設計為可拖曳,請設定
GMSMarker.draggable
屬性。 - 如要設定標記的說明文字,請使用
GMSMarker.title
屬性。
回應標記事件
如要回應標記事件,您可以在檢視畫面中加入 GMSMapViewDelegate
通訊協定,並導入對應的回呼。本例說明所選標記的 title
和 snippet
。
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;