我想将标记及其圆圈固定在 map 的中心,而不管使用 swift 的位置坐标。如果用户在 map 上移动相机,我希望它继续显示在中心,而没有任何半径或标记闪烁,并且用户应该能够增加和减少圆的半径。
我使用了 circle.position = mapView.camera.target 但它没有帮助。
所以我想知道我该怎么做?
请帮忙。
最佳答案
您的 ViewController
类需要从 func mapView(mapView: GMSMapView!, didChangeCameraPosition position: GMSCameraPosition!)
实现 GMSMapViewDelegate
方法。
在 func mapView(mapView: GMSMapView!, didChangeCameraPosition position: GMSCameraPosition!)
方法中,您可以更新圆圈的位置。该方法在 map 上的任何动画或手势期间被重复调用(或一次,如果相机被明确设置)。
示例代码:
class ViewController: UIViewController, GMSMapViewDelegate {
var mapView: GMSMapView!
var cirlce: GMSCircle!
override func viewDidLoad() {
super.viewDidLoad()
let camera = GMSCameraPosition.cameraWithLatitude(-33.86,
longitude: 151.20, zoom: 6)
mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
mapView.myLocationEnabled = true
self.view = mapView
mapView.delegate = self
cirlce = GMSCircle(position: camera.target, radius: 100000)
cirlce.fillColor = UIColor.redColor().colorWithAlphaComponent(0.5)
cirlce.map = mapView
}
func mapView(mapView: GMSMapView!, didChangeCameraPosition position: GMSCameraPosition!) {
print("\(position.target.latitude) \(position.target.longitude)")
cirlce.position = position.target
}
}
编辑:
如果您想制作一个与您的 mapView 相机移动无关的圆圈,您可以在您的 mapView 顶部制作一个圆形 UIView。
示例代码:
class ViewController: UIViewController {
var mapView: GMSMapView!
override func viewDidLoad() {
super.viewDidLoad()
let camera = GMSCameraPosition.cameraWithLatitude(-33.86,
longitude: 151.20, zoom: 6)
mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
mapView.myLocationEnabled = true
self.view = mapView
let circleView = UIView()
circleView.backgroundColor = UIColor.redColor().colorWithAlphaComponent(0.5)
view.addSubview(circleView)
view.bringSubviewToFront(circleView)
circleView.translatesAutoresizingMaskIntoConstraints = false
let heightConstraint = NSLayoutConstraint(item: circleView, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100)
let widthConstraint = NSLayoutConstraint(item: circleView, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100)
let centerXConstraint = NSLayoutConstraint(item: circleView, attribute: .CenterX, relatedBy: .Equal, toItem: view, attribute: .CenterX, multiplier: 1, constant: 0)
let centerYConstraint = NSLayoutConstraint(item: circleView, attribute: .CenterY, relatedBy: .Equal, toItem: view, attribute: .CenterY, multiplier: 1, constant: 0)
NSLayoutConstraint.activateConstraints([heightConstraint, widthConstraint, centerXConstraint, centerYConstraint])
view.updateConstraints()
UIView.animateWithDuration(1.0, animations: {
self.view.layoutIfNeeded()
circleView.layer.cornerRadius = CGRectGetWidth(circleView.frame)/2
circleView.clipsToBounds = true
})
}
}
关于ios - 如何修复 Google Maps ios 中心的标记和 GMSCircle?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32733435/