标记事件和手势

设置特定的高级标记属性后,您可以监控标记事件,如点按和手势。如果点按标记,可以查看其他信息,例如标记标题或摘要。用户还可以使用长按手势移动可拖动的标记。

响应标记事件

您可以通过向视图添加 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;