【发布时间】:2015-08-06 02:42:34
【问题描述】:
我正在尝试将 Google 地图置于标记的中心,并在用户点击标记时进行缩放。我可以在 Android 中使用点击委托来执行此操作,但我无法在 Swift 中找到执行此操作的方法。
有没有人有任何提示或建议来解决这个问题?
【问题讨论】:
标签: swift
我正在尝试将 Google 地图置于标记的中心,并在用户点击标记时进行缩放。我可以在 Android 中使用点击委托来执行此操作,但我无法在 Swift 中找到执行此操作的方法。
有没有人有任何提示或建议来解决这个问题?
【问题讨论】:
标签: swift
你必须符合 GMSMapViewDelegate 协议,然后你有下面的功能:
func mapView(mapView: GMSMapView, didTapMarker marker: GMSMarker) -> Bool {
//you can handle zooming and camera update here
}
您可以通过为多个位置创建边界来更新相机,例如:(您也可以为边界提供填充)
let bounds = GMSCoordinateBounds(coordinate: self.userLocation!.coordinate, coordinate: marker.position)
self.mapView.animateWithCameraUpdate(GMSCameraUpdate.fitBounds(bounds, withPadding: 120.0))
您还可以使用缩放级别的一个位置更新相机位置,例如:
self.mapView?.camera = GMSCameraPosition.cameraWithTarget(marker.position, zoom: 9.0)
【讨论】:
在 swift4 中点击标记:
func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
// your code.
}
【讨论】:
我将分部分回答你的问题:
将 Google 地图置于标记的中心:
let camera = GMSCameraPosition.cameraWithLatitude(lat, longitude: long, zoom: 8.0)
self.mapView.camera = camera
lat, long 是你放置标记的地方的坐标。
用户点击标记时缩放:
func mapView(mapView: GMSMapView, didTapMarker marker: GMSMarker) -> Bool {
let update = GMSCameraUpdate.zoomBy(2)
mapView.animateWithCameraUpdate(update)
}
另外,请确保您的班级符合GMSMapViewDelegate。
【讨论】: