Search code examples
iosswiftgoogle-maps-sdk-ios

GoogleMaps camera target in top left corner of map iOS since pod update


I have an app that I use to drop pins on a GoogleMap and then save those points to a Firebase database, to then use as reference for end users in another app.

I recently updated the pods in my application to Using GoogleMaps (3.3.0). the map was working fine previously, but now when I go to the map it starts at what I presume to be lat/long 0,0. as soon as I interact with the map (like dragging it), it pops into whatever target I had designated it to start from. additionally, when I drop pins, which I designate to drop on camera.target, it used to drop in the center as expected. the markers now drop on the top left corner of the map. The code I used previously worked fine and I get no errors at build or runtime. it seems camera.target seems to be

I've seen 'fleeting' references to it here: https://github.com/flutter/flutter/issues/24806

and here: Google Map Centers at top left corner

but in neither case does the situation seem to be exactly the same.

Edit: rotating the phone to landscape and back seems to reset everything correctly, which makes it seem like it's a view issue, not a maps issue or a loading timing issue, although when the map starts at 0,0 that lends itself to more of a loading timing issue.

if this was a bug with the pod, I'd assume I'd find it all over stack, but I struggle to see how it's an issue with my code, as it was working just fine, and I don't see any changes in the walk through of the SDK that insinuate my code is now somehow flawed.

Loading the mapView, which is created using IB override func viewDidLoad() { super.viewDidLoad()

let ud = UserDefaults.standard
    let startingX = ud.double(forKey: "startingX")
    let startingY = ud.double(forKey: "startingY")

    let camera = GMSCameraPosition.camera(withLatitude: startingX, longitude: startingY, zoom: 18.0)
    googleMapView.camera = camera

    self.googleMapView.mapType = .hybrid

    self.googleMapView.settings.scrollGestures = true
    self.googleMapView.settings.rotateGestures = true
    self.googleMapView.settings.consumesGesturesInView = true
}

When I create a marker:

@IBAction func createPinButtonPressed(_ sender: Any) {

    if self.CoordinateSelectorSegment.selectedSegmentIndex == 0
    {

        if backTeeMarker.map == nil {
        backTeeMarker.position = self.googleMapView.camera.target
        backTeeMarker.title = "back tee"
        backTeeMarker.map = googleMapView
        backTeeMarker.isDraggable = true
        backTeeMarker.icon = GMSMarker.markerImage(with: .red)
        }
    }
...
}

Solution

  • I found I was able to solve this problem by adding the map creation to ViewWillAppear as opposed to having it in the ViewDidLoad.

     override func viewWillAppear(_ animated: Bool) {
        let ud = UserDefaults.standard
        let startingX = ud.double(forKey: "startingX")
        let startingY = ud.double(forKey: "startingY")
    
        let camera = GMSCameraPosition.camera(withLatitude: startingX, longitude: startingY, zoom: 18.0)
        googleMapView.camera = camera
    
        self.googleMapView.mapType = .hybrid
    
        self.googleMapView.settings.scrollGestures = true
        self.googleMapView.settings.rotateGestures = true
        self.googleMapView.settings.consumesGesturesInView = true
    }