Search code examples
iosswiftxcodegmsmapview

Swift - GMSMapView animate(toLocation:) / animate(to:CameraPositon) not working


This is how I init the map - GMSMapView

class ViewController: UIViewController {
   var map = GMSMapView()

   override func viewDidLoad() {
       super.viewDidLoad()
       setupGoogleView()
   }

   private func setupGoogleView() {
       guard let coordinates = getUserLocation() else { return }
       let camera = GMSCameraPosition.camera(withLatitude: coordinates.latitude, longitude: coordinates.longitude, zoom: 16.0)
       self.map = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
       map.settings.tiltGestures = false
       map.mapType = .satellite
       map.delegate = self
       map.frame = view.frame
       self.view.addSubview(map)
   }
}

The problem comes when I call this function from somewhere else in the file

private func animateTo(location: CLLocationCoordinate2D) {
    DispatchQueue.main.async {
        let cameraPosition = GMSCameraPosition(target: location, zoom: 20)
        self.map.animate(toLocation: location)
        self.map.animate(to: cameraPosition)
    }
}

I am trying to relocate the camera to some coordinates, but nothing happens. I have tried every solution on stackoverflow and google.

I have lat and lng in the location - checked. The function is called - checked.

I have also tried to self.view.layoutSubviews(), self.view.layoutIfNeeded(), and also for map


Solution

  • I manage to solve the issue with this:

    self.view.subviews.forEach { (view) in
       if let mapView = view as? GMSMapView {
          // do update on mapView here
       }
    }