【发布时间】:2017-05-09 15:27:39
【问题描述】:
我有一个关于标记的小问题。我应该怎么做才能让用户在我的基于谷歌地图的应用程序上放置自己的标记?
【问题讨论】:
标签: ios swift google-maps markers
我有一个关于标记的小问题。我应该怎么做才能让用户在我的基于谷歌地图的应用程序上放置自己的标记?
【问题讨论】:
标签: ios swift google-maps markers
有多种方法可以做到这一点。其中之一是在用户长按地图时添加标记。要检测长按,请实现此委托方法:
func mapView(_ mapView: GMSMapView, didLongPressAt coordinate: CLLocationCoordinate2D) {
let marker = GMSMarker(position: coordinate)
// marker.isDraggable = true
// marker.appearAnimation = kGMSMarkerAnimationPop
marker.map = mapView
// marker.icon = GMSMarker.markerImage(with: UIColor.blue)
}
注释掉的行是可选的,因为它们只是设置标记的自定义属性。您可以自定义更多内容。
另外,如果您还没有这样做,请将GMSMapViewDelegate 添加到您的视图控制器类的声明中:
class YourViewController: UIViewController, GMSMapViewDelegate {
并将self 分配给delegate 属性:
let camera = GMSCameraPosition.camera(withLatitude: 0, longitude: 0, zoom: 3)
let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
mapView.isMyLocationEnabled = true
view = mapView
mapView.delegate = self // add this line!
或者,您可以在其他事件发生时添加标记。例如,您可以在用户点击UIBarBUttonItem 或UIButton 时添加标记。全取决于你!但是添加按钮的过程基本上就是这两行:
let marker = GMSMarker(position: coordinate)
marker.map = mapView
// mapView is the map that you want to add the marker to. If you are doing this outside a delegate method, use self.view
您还可以考虑将标记添加到集合中,以便以后修改它们。
【讨论】: