设置特定的高级标记属性后,您可以监控标记事件,例如点按和手势。点按标记后,用户可以看到额外的信息,例如标记标题或信息摘要。用户还可以使用长按手势移动可拖动标记。
- 如需跟踪标记事件,请将
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;