Moving Google Maps Camera to a Location
Asked Answered
A

6

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?

Ansilma answered 13/5, 2017 at 1:59 Comment(0)
C
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!

Cardioid answered 15/5, 2017 at 13:28 Comment(2)
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?Intendment
@Intendment 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.Panhandle
I
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

Interpretation answered 11/2, 2018 at 21:10 Comment(0)
S
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
    }
Septuplicate answered 13/5, 2017 at 4:49 Comment(0)
F
1

For Objective-c the method is:

[mapView moveCamera:[GMSCameraUpdate setTarget:<CLLocationCoordinate2DMake>]];
Foreclosure answered 9/10, 2018 at 10:16 Comment(0)
S
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

Slipknot answered 25/9, 2020 at 18:47 Comment(0)
P
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)
    }
Pul answered 12/8, 2022 at 13:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.