25

I'm working with a Google Maps View and I want to add a button to the map that when tapped, will move the camera to a specific location. I currently have a button outlet and an action connected to the button.

@IBAction func locationTapped(_ sender: Any) {
    print("tapped")
    let location = GMSCameraPosition.camera(withLatitude: place.latitude, longitude: place.longitude, zoom: 17.0)

    mapView.camera = location
}

place exists but for some reason, the camera will not budge. I've tried different versions of code and looked at the Google Maps documentation but none of the options are producing results. Can anyone tell me what I'm doing wrong?

ch1maera
  • 1,369
  • 5
  • 20
  • 42

6 Answers6

41

The GMSMapView class has the following function:

animate(to: GMSCameraPosition)

So in your code sample, instead of doing this:

mapView.camera = location

Try doing this:

mapView.animate(to: location)

Hope this helps!

Pheepster
  • 6,045
  • 6
  • 41
  • 75
  • Hi, We tried to implement this, but the camera doesn't move unless the user interacts with the map. As soon as the user touches/taps map, the camera moves to the desired location. Did we miss something? – shoan Aug 22 '19 at 07:52
  • @shoan I am facing same weird thing. Did you got any solutions, please let me know. I am not able to figure it out wits the problem here. – shashi Gupta Jul 27 '21 at 08:41
19

in Swift3 and Swift4 for moving marker to current position use this:

func myLocationBtnAction(_ sender: UIButton) {
            mapView.moveCamera(GMSCameraUpdate.setTarget(CLLocationCoordinate2D(latitude: (mapView.myLocation?.coordinate.latitude)!, longitude: (mapView.myLocation?.coordinate.longitude)!), zoom: 16))

and for a specific location use this:

let camera = GMSCameraPosition.camera(withLatitude: lat, longitude: lng, zoom: 16)
            mapView?.camera = camera
            mapView?.animate(to: camera)

and don't forget to extend GMSAutocompleteViewControllerDelegate for current location

Mohsen mokhtari
  • 2,841
  • 1
  • 30
  • 39
3

Swift 2.3

This code is used for my purpose. In which marker tap event used, which moves camera position of map. Hope you find your solution.

 func mapView(mapView: GMSMapView, didTapMarker marker: GMSMarker) -> Bool {
        mapView.selectedMarker = marker
        var point = mapView.projection.pointForCoordinate(marker.position)
        let camera = mapView.projection.coordinateForPoint(point)
        let position = GMSCameraUpdate.setTarget(camera)
        mapView.animateWithCameraUpdate(position)
        return true
    }
Jaydeep Vora
  • 6,085
  • 1
  • 22
  • 40
1

For Objective-c the method is:

[mapView moveCamera:[GMSCameraUpdate setTarget:<CLLocationCoordinate2DMake>]];
Er. Vihar
  • 1,495
  • 1
  • 15
  • 29
0

Maybe this is to late but i resolve that problem with adding this:

DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(500), execute: {
        let camera = GMSCameraPosition.camera(withLatitude: lat!, longitude: lon!, zoom: 17.0)
        self.mMap.animate(to: camera)
    })

You have to wait until map load delegate

Angel Ruiz
  • 31
  • 6
0
   func moveMapCamera(at cordinate: CLLocationCoordinate2D, animated: Bool = false) {
        let camera = MKMapCamera()
        camera.centerCoordinate = cordinate
        camera.pitch = 0
        camera.altitude = 9000
        mapV.setCamera(camera, animated: animated)
    }