使用Swift限制iOS版Google Maps中的平移 [英] Limiting panning in Google Maps for iOS with Swift

查看:183
本文介绍了使用Swift限制iOS版Google Maps中的平移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了使用JS而不是Swift的建议.

I have seen suggestions for this using JS but not Swift.

如何快速使用iOS版Google Maps SDK将平移限制在覆盖图像的边缘?我有一个覆盖特定区域的地图叠加层图像,只希望用户能够平移到该叠加层图像的边缘.我以为这很简单,但显然不是这样!

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.

到目前为止,在mapController中,我已经在覆盖层周围绘制了一个多边形,以确保我知道该覆盖层图像的确切范围和坐标.

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.

我不确定是否可以通过内置的方式快速完成& google maps API,这似乎效率很低.

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)
    }

这很糟糕,因为

  1. 它无限地向上摇动
  2. '东部'是边界-击中边界时不是相机的中心.我无法以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.

推荐答案

我终于可以通过遵循此实现并将其从Objective-C更改为Swift来解决此问题:

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?

唯一的问题是currentPosition集中在地图的中心而不是地图的边缘,因此我将叠加图像放大了以解决这个问题.

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.

这篇关于使用Swift限制iOS版Google Maps中的平移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆