限制 MKMapView 滚动
我正在尝试将自定义图像作为 MKOverlayView
添加到 MKMapView
中 - 我需要限制用户能够滚动到叠加层的边界之外。是否有现有的功能可以做到这一点?或者还有其他建议吗?
谢谢,马特
I'm trying to add a custom image to an MKMapView
as an MKOverlayView
- I need to restrict users from being able to scroll outside the bounds of the overlay. Are there any existing functions to do this? Or any other suggestions?
Thanks, Matt
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果您只想将地图视图冻结在叠加层上,则可以将地图视图的区域设置为叠加层的边界,并将
scrollEnabled
和zoomEnabled
设置为NO
代码>.但这不会让用户在覆盖层的边界内滚动或缩放。
没有内置方法可以将地图视图限制在叠加层的边界内,因此您必须手动执行此操作。首先,确保您的
MKOverlay
对象实现boundingMapRect
属性。然后可以在regionDidChangeAnimated
委托方法中使用它来根据需要手动调整视图。以下是如何完成此操作的示例。
下面的代码应该位于具有
MKMapView
的类中。确保地图视图最初设置为叠加层可见的区域。
我用内置的 MKCircle 覆盖层尝试了这一点,似乎效果很好。
编辑:
它确实在 95% 的情况下工作良好,但是,我通过一些测试确认它可能会在两个位置之间振荡,然后进入无限循环。因此,我对其进行了一些编辑,我认为这应该可以解决问题:
并且以防万一有人正在寻找快速
MKOverlay
解决方案,这里有一个:If you just want to freeze the map view at the overlay, you could set the map view's region to the overlay's bounds and set
scrollEnabled
andzoomEnabled
toNO
.But that won't let the user scroll or zoom inside the overlay's bounds.
There aren't built-in ways to restrict the map view to the overlay's bounds so you'd have to do it manually. First, make sure your
MKOverlay
object implements theboundingMapRect
property. That can then be used in theregionDidChangeAnimated
delegate method to manually adjust the view as needed.Here's an example of how this could be done.
Code below should be in the class that has the
MKMapView
.Make sure the map view is initially set to a region where the overlay is visible.
I tried this with the built-in
MKCircle
overlay and seems to work well.EDIT:
It does work well 95% of the time, however, I have confirmed through some testing that it might oscillate between two locations, then enter an infinite loop. So, I edited it a bit, I think this should solve the problem:
And just in case someone is looking for a quick
MKOverlay
solution, here is one:就我而言,我需要将边界限制为具有左上/右下坐标的平铺覆盖。上面的代码仍然可以正常工作,但用 theOverlay.boundingMapRect 替换 MKMapRect paddedBoundingMapRect
}
In my case, I needed to restrict bounds to tiled overlay which has an upperleft / lowerRight coordinates. Code above still works well, but substituted theOverlay.boundingMapRect for MKMapRect paddedBoundingMapRect
}
MapKit 现在在 iOS 13 中原生执行此操作
。您可以显式设置边界来限制平移。
请参阅2378 秒的 WWDC 2019 视频了解演示。
您还可以限制缩放级别
参考
MapKit now does this natively in iOS 13
You can explicitly set a boundary to restrict panning.
See WWDC 2019 video at 2378 seconds for a demonstration.
You can also restrict zoom levels
References
使用以下代码,您可以检测滚动的边界限制
注意:在以下代码中,5000 数字是以米为单位的限制区域量。所以你可以像这样使用>
let restricedAreaMeters = 5000
最后使用下面的重写方法,如果用户离开有界区域,将返回到最新允许的坐标。
with following code you can detect bound limit for scroll
NOTE: in following code 5000 number is amount of restriced area in terms of meters. so you can use like this >
let restricedAreaMeters = 5000
and finally with overrided method at bellow if user got out from bounded area will be returned to latest allowed coordinate.
在 mapViewDidFinishLoadingMap 内部使用的SWIFT 5
简单解决方案:
SWIFT 5
simple solution for use inside mapViewDidFinishLoadingMap:
Swift 3.0 中的 Anna (https://stackoverflow.com/a/4126011/3191130) 解决方案,我添加到了扩展中:
要设置地图,请使用以下命令:
Anna's (https://stackoverflow.com/a/4126011/3191130) solution in Swift 3.0, I added to an extension:
To setup the map use this: