Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update a GMSMapView with GMSCameraUpdate

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.

like image 950
Peter Todd Avatar asked Feb 09 '23 10:02

Peter Todd


1 Answers

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;
like image 94
Peter Todd Avatar answered Feb 13 '23 04:02

Peter Todd