Limiting panning in Google Maps for iOS with Swift

纵然是瞬间 提交于 2019-12-23 03:06:44

问题


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

  1. It pans upward infinitely
  2. '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.


回答1:


I was finally able to solve this by following this implementation and changing it from Objective-C to Swift: How do I prevent 'GMSMapView' from Infinite horizontal scrolling?

The only issue is the currentPosition is focused on the centre of the map rather than the edges of the map, so I made my overlay image larger to account for this.



来源:https://stackoverflow.com/questions/35328808/limiting-panning-in-google-maps-for-ios-with-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!