-4
我有这个标记简介小问题。我应该怎么做才能让用户在我的基于谷歌地图的应用上放置自己的标记?如何让用户放置标记在地图上?
我有这个标记简介小问题。我应该怎么做才能让用户在我的基于谷歌地图的应用上放置自己的标记?如何让用户放置标记在地图上?
有多种方法可以做到这一点。其中之一是在用户长按地图时添加标记。为了检测长按,实现这个委托方法:
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
您还可以考虑将标记添加到集合,以便稍后修改它们。
如果你觉得我的答案回答你的问题,请考虑一下对对号接受它! @情欲 – Sweeper