Reputation: 2491
Currently I set marker as draggable and user can drag it within the current map camera. When the marker reaches the screen edges, the camera does not move to make for a continuous drag experience. How to make the Google maps camera move when the marker reaches the screen edge?
// Is not working as the marker and map keeps moving
func mapView(_ mapView: GMSMapView, didDrag marker: GMSMarker) {
let camera = GMSCameraPosition(target: marker.position, zoom: zoom)
self.mapView.map.animate(to: camera)
}
Upvotes: 1
Views: 2611
Reputation: 631
You used the right function but first you need to check wether the current marker position is with in bound of the mapView or not. If it isn't in the boundary range then we have to set the camera position.
Replace you didDrag function with the below code
func mapView(_ mapView: GMSMapView, didDrag marker: GMSMarker) {
if !isMarkerWithinScreen(marker: marker) {
let camera = GMSCameraPosition(target: marker.position, zoom: mapViewForScreen.camera.zoom)
self.mapView.animate(to: camera)
}
}
Add this function to your viewController to check wether marker is out of bound or not.
func isMarkerWithinScreen(marker: GMSMarker, _ mapView: GMSMapView) -> Bool {
let region = self.mapView.projection.visibleRegion()
let bounds = GMSCoordinateBounds(region: region)
return bounds.contains(marker.position)
}
I already done this. Feel free to ask if you are still facing this issue.
Upvotes: 2