Move Google Maps camera to location

I am working with Google Maps View and I want to add a button to the map that, when clicked on it, will move the camera to a specific location. I currently have a button and an action associated with a 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 the code and looked at the Google Maps documentation, but none of the options are producing results. Can anyone tell me what I am doing wrong?

+3


source to share


3 answers


The GMSMapView class has the following function:

animate (to: GMSCameraPosition)

So, in your example code, instead of this:

mapView.camera = location

      



Try the following:

mapView.animate(to: location)

      

Hope this helps!

+8


source


in Swift3 and Swift4 to move the marker to the 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 place use this:



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

      

and don't forget to expand for the current location GMSAutocompleteViewControllerDelegate

+5


source


Swift 2.3

This code is used for my purpose. Which uses a marker cue event that moves the position of the camera on the map. I 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
    }

      

0


source







All Articles