I have seen suggestions for this using JS but not Swift.
How can I limit panning to the edges of an overlay image using Google Maps SDK for iOS with swift? I have a map overlay image covering a specific area, and only want the user to be able to pan to the edges of this overlay image. I thought this would be very simple, but apparently not!
So if they pan right, and hit the east edge of the overlay image, they can't pan any further in that direction.
So far, in the mapController, I have drawn a polygon around the overlay to make sure I know the exact bounds and co-ordinates of that overlay image.
// Create a rectangular path
let rect = GMSMutablePath()
rect.addCoordinate(CLLocationCoordinate2DMake(south, east)); //South-East
rect.addCoordinate(CLLocationCoordinate2DMake(north, east)); //North-East
rect.addCoordinate(CLLocationCoordinate2DMake(north, west)); //North-West
rect.addCoordinate(CLLocationCoordinate2DMake(south, west)); //South-West
// Create the polygon, and assign it to the map.
let polygon = GMSPolygon(path: rect)
polygon.fillColor = UIColor(red:0.25, green:0, blue:0, alpha:0.05);
polygon.strokeColor = UIColor.blackColor()
polygon.strokeWidth = 2
polygon.map = mapView
I have got as far as detecting if the north, east, west, or south edge of the image is hit as the user is panning, and printing to console when this happens.
I am unsure of a built-in way to do this with swift & the google maps API and this seems really inefficient.
//run when camera changes position
func mapView(mapView: GMSMapView!, didChangeCameraPosition position: GMSCameraPosition){
if(mapView.projection.visibleRegion().farRight.longitude >= east)
{
... print notice ...
}
else if(mapView.projection.visibleRegion().farRight.latitude >= north){
... print notice ...
}
else if(mapView.projection.visibleRegion().farLeft.longitude <= west){
... print notice ...
}
else if(mapView.projection.visibleRegion().nearLeft.latitude <= south){
... print notice ...
}
else{
... print SE, NE, NW, and SW co-ords ...
}
}
Finally, if an overlay edge is hit, I want it to stop panning in this direction. I have tried to adapt these instructions for panning within a certain radius, but to no avail:
if(mapView.projection.visibleRegion().farRight.longitude >= east)
{
... print notice ...
//Limit the panning co-ords by the East edge
let target: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: mapView.projection.visibleRegion().farRight.latitude, longitude: east)
//Pass this limit to the camera
let camera: GMSCameraPosition = GMSCameraPosition(target: target, zoom: mapView.camera.zoom, bearing: 0, viewingAngle: 0)
mapView.animateToCameraPosition(camera)
}
This is bad because
- It pans upward infinitely
- 'east' is the boundary - not the center of the camera at the time the boundary is hit. I cannot get this value in a way that will be accepted by CLLocationCoordinate2D.
There must be an easier way! This seems really complicated for something I thought was a common use case. I don't know how then make this deal with zooming, rotating, hitting the north & east edge at the same time, etc.