1

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

Toto
  • 593
  • 7
  • 20

2 Answers2

0

try to use CLLocationManagerDelegate

// Handle authorization for the location manager.
    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
        switch status {
        case .restricted:
            print("Location access was restricted.")
        case .denied:
            print("User denied access to location.")
            // Display the map using the default location.
            mapView.isHidden = false
        case .notDetermined:
            print("Location status not determined.")
        case .authorizedAlways: fallthrough
        case .authorizedWhenInUse:
            print("Location status is OK.")
            getFilialList()
        @unknown default:
            fatalError()
        }
    }

then

 // Handle incoming location events.
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let location: CLLocation = locations.last!
        print("Location: \(location)")
        let camera = GMSCameraPosition.camera(withLatitude: location.coordinate.latitude,
                                              longitude: location.coordinate.longitude,
                                              zoom: 12)
        DispatchQueue.main.async {
            self.mapView.animate(to: camera)
            self.locationManager.stopUpdatingLocation()
        }
    }
swiftHelp
  • 56
  • 3
  • I'm handling the `didChangeAuthorization` separately, I don't see the problem there. I have added the mapView to the self.view, it's accessible and the user can zoom and everything, but when a custom buttom I added on top of the self.view is tapped, even for changing the map type, the actual mapView doesn't actualise(is like doesn't receive the commands) – Toto Feb 07 '20 at 09:13
0

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
   }
}
Toto
  • 593
  • 7
  • 20