user7219266
user7219266

Reputation:

Why GMSMapView is not set at the appropriate coordinate?

This might be a frequently asked question, but believe it or not, I didn't find an answer that helped me fix my issue.

Nevertheless I tried multiple solutions and I don't understand why sometimes the camera of the GMSMapView object does not initialize at the right position.

I have dragged an UIView in my UIViewController scene from the Interface Builder, set it as GMSMapView object.

Here is my setCamera method :

private func setCamera(lat: Double, long: Double, zoom: Float) {

    let camera = GMSCameraPosition.camera(withLatitude: lat, longitude: long, zoom: zoom)
    mapView.camera = camera
    mapView.setMinZoom(5, maxZoom: 15)
}

I tried to call it in viewDidLoad or viewWillAppear it's the same behavior.

1 out of 5 launches (approximately), the camera is not centered at the right coordinate, it feels like the GMSMapView is initialized with random camera.

How can I fix it ?

Upvotes: 0

Views: 99

Answers (1)

IBAction
IBAction

Reputation: 2207

It's my initialization of GMSMapView:

let camera = GMSCameraPosition.camera(withTarget: myLocation.coordinate, 
                                      zoom: mapDefaultZoom)
let mapView = GMSMapView.map(withFrame: view.bounds, 
                             camera: camera)
view.addSubview(mapView)

It's update of the camera position:

let bounds = GMSCoordinateBounds().includingCoordinate(position)
let update = GMSCameraUpdate.fit(bounds, 
                                 withPadding: 60)
mapView.animate(with: update)

Hope, it will help you.

Upvotes: 2

Related Questions