我想在地图的中心修复标记及其圆圈,而不管使用swift的位置坐标。如果用户在地图上移动相机,我希望它在中心不断出现任何闪烁,或者标记和用户应该能够增加和减少圆的半径。 我使用了
circle.position = mapView.camera.target,但没有用。 所以我想知道我该怎么做? 请帮忙。
答案 0 :(得分:6)
您的ViewController
课程需要实施func mapView(mapView: GMSMapView!, didChangeCameraPosition position: GMSCameraPosition!)
中的GMSMapViewDelegate
方法。
在func mapView(mapView: GMSMapView!, didChangeCameraPosition position: GMSCameraPosition!)
方法中,您可以更新圆圈的位置。在地图上的任何动画或手势期间重复调用该方法(或者,如果明确设置了相机,则重复一次)。
示例代码:
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
})
}
}
答案 1 :(得分:1)
import UIKit
import GoogleMaps
class ViewController: UIViewController, GMSMapViewDelegate {
var mapView: GMSMapView!
override func viewDidLoad() {
super.viewDidLoad()
let camera = GMSCameraPosition.camera(withLatitude: -33.86,
longitude: 151.20, zoom: 6)
mapView = GMSMapView.map(withFrame: .zero, camera: camera)
mapView.isMyLocationEnabled = true
self.view = mapView
mapView.delegate = self
let circleView = UIView(frame: CGRect(x: 0, y: 0, width: 20, height: 10))
circleView.backgroundColor = UIColor.red.withAlphaComponent(0.5)
view.addSubview(circleView)
view.bringSubview(toFront: 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.activate([heightConstraint, widthConstraint, centerXConstraint, centerYConstraint])
view.updateConstraints()
UIView.animate(withDuration: 1.0, animations: {
self.view.layoutIfNeeded()
circleView.layer.cornerRadius = circleView.frame.width/2
circleView.clipsToBounds = true
})
}
func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) {
print("\(position.target.latitude) \(position.target.longitude)")
}
}