Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I fix marker and GMSCircle on the center in Google Maps ios?

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.target
but it didn't help. So I wanna know how can I do that? Please help.
like image 400
Aaqib Hussain Avatar asked Sep 23 '15 07:09

Aaqib Hussain


2 Answers

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
    }

}

enter image description here

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
        })
    }
}

enter image description here

like image 99
ztan Avatar answered Nov 09 '22 22:11

ztan


This is an updated version to Swift 3

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)")
     } 
}
like image 45
Mohsen Avatar answered Nov 09 '22 22:11

Mohsen