【发布时间】:2016-05-04 11:36:59
【问题描述】:
如何确保 GMS 标记保持固定在地图中心,只允许拖动地图而不允许拖动标记?
谢谢
【问题讨论】:
-
只需在地图中心添加标记(这是 Saqib 建议的).. 没有理由额外强调重新计算标记的坐标以使其保持居中
标签: swift google-maps google-maps-markers
如何确保 GMS 标记保持固定在地图中心,只允许拖动地图而不允许拖动标记?
谢谢
【问题讨论】:
标签: swift google-maps google-maps-markers
首先从 CLLocationManager 获取中心位置,然后使用 GMS Marker 放置标记。
为了获取当前位置符合 CLLcationManagerDelegate 并使用以下函数获取您的位置坐标:
locationManager.startUpdatingLocation()
locationManager.delegate = self
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = manager.location else {
return
}
manager.stopUpdatingLocation()
// place marker to center of the map
let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
}
【讨论】:
您可以添加垂直和水平约束来对齐标记视图。
let xConstraint = NSLayoutConstraint(item: YourMarkerView, attribute: .CenterX, relatedBy: .Equal, toItem: self.YourMapView, attribute: .CenterX, multiplier: 1, constant: 0)
let yConstraint = NSLayoutConstraint(item: YourMarkerView, attribute: .CenterY, relatedBy: .Equal, toItem: self.YourMapView, attribute: .CenterY, multiplier: 1, constant: 0)
self.YourMapView.addConstraint(xConstraint)
self.YourMapView.addConstraint(yConstraint)
还要确保您已将约束添加到 YourMapView。 (您可以将它们添加到情节提要中。)
【讨论】: