How to update a GMSMapView with GMSCameraUpdate
Asked Answered
F

1

5

I call animateWithCameraUpdate on a GMSMapView expecting it to change the map view to show the new GMSCoordinateBounds but it has no effect.

My map loads in a UICollectionViewReusableView to initially display Western Europe:

@IBOutlet weak var mapView: GMSMapView!

var fetchedResultsController : NSFetchedResultsController!


override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
    let camera = GMSCameraPosition.cameraWithLatitude(51.48, longitude: 0, zoom: 4)
    mapView.camera = camera

}

I then call a function to query all my locations and update the GMSMapView to show all my locations:

func plotAll(){
    let bounds = GMSCoordinateBounds.init()
    for property in fetchedResultsController.fetchedObjects!
    {
        let capitalAsset : CapitalAsset = property as! CapitalAsset
        let marker = GMSMarker.init()
        marker.draggable = false
        marker.snippet = capitalAsset.address
        let location = CLLocationCoordinate2DMake(Double(capitalAsset.latitude!), Double(capitalAsset.longitude!))
        marker.position = location
        marker.map = mapView
        // Update bounds to include marker
        bounds.includingCoordinate(marker.position)
    }

    mapView.animateWithCameraUpdate(GMSCameraUpdate.fitBounds(bounds, withPadding: 50.0))

}

My plotAll function is successfully called and loops through a dozen global locations adding these to the GMSCoordinateBounds.

But the map is not being updated. I was expecting the map view to change when I called animateWithCameraUpdate but it has no effect.

Further information, for debugging I replaced the line

    mapView.animateWithCameraUpdate(GMSCameraUpdate.fitBounds(bounds, withPadding: 50.0))

with :

let camera = GMSCameraPosition.cameraWithLatitude(51.48, longitude: 0, zoom: 10)
mapView.camera = camera

This does update my map view so there is no problem with calling my plotAll function, the issue is probably in my use of animateWithCameraUpdate.

Fleischer answered 28/11, 2015 at 13:12 Comment(0)
F
9

I got this to work by replacing :

mapView.animateWithCameraUpdate(GMSCameraUpdate.fitBounds(bounds, withPadding: 50.0))

with:

let camera = mapView.cameraForBounds(bounds, insets:UIEdgeInsetsZero)
mapView.camera = camera;
Fleischer answered 28/11, 2015 at 13:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.