I want to fix the marker and the circle of it in the center of the map irrespective of the location coordinates using swift. If the user moves the camera on map I want it to keep showing up in the center without any flickering in radius or the marker and user should be able to increase and decrease the radius of the circle. I used
circle.position = mapView.camera.targetbut it didn't help. So I wanna know how can I do that? Please help.
Your ViewController
class needs to implement the func mapView(mapView: GMSMapView!, didChangeCameraPosition position: GMSCameraPosition!)
method from GMSMapViewDelegate
.
Inside the func mapView(mapView: GMSMapView!, didChangeCameraPosition position: GMSCameraPosition!)
method, you can update the position of your circle. The method is called repeatedly during any animations or gestures on the map (or once, if the camera is explicitly set).
Sample code:
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
}
}
Edited:
If you want to make a circle irrelevant to your mapView camera movement, you can make a circular UIView on top of your mapView.
Sample code:
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
})
}
}
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)")
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With