有没有办法限制 MKMapView 最大缩放级别?
问题是 - 有没有办法限制 MKMapView 的最大缩放级别?或者有没有办法跟踪用户何时缩放到没有可用地图图像的级别?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
问题是 - 有没有办法限制 MKMapView 的最大缩放级别?或者有没有办法跟踪用户何时缩放到没有可用地图图像的级别?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(12)
您可以使用
mapView:regionWillChangeAnimated:
委托方法来侦听区域更改事件,如果该区域比最大区域宽,请使用setRegion:animated将其设置回最大区域:
向您的用户表明他们无法缩小那么远。方法如下:You could use the
mapView:regionWillChangeAnimated:
delegate method to listen for region change events, and if the region is wider than your maximum region, set it back to the max region withsetRegion:animated:
to indicate to your user that they can't zoom out that far. Here's the methods:如果您仅使用 iOS 7+,则可以获取/设置一个新的
camera.altitude
属性来强制执行缩放级别。它相当于 azdev 的解决方案,但不需要外部代码。在测试中,我还发现,如果您反复尝试放大细节,则可能会进入无限循环,因此我在下面的代码中使用了一个 var 来防止这种情况发生。
If you're working with iOS 7+ only, there's a new
camera.altitude
property that you can get/set to enforce a zoom level. Its equivalent to azdev's solution, but no external code is required.In testing, I also discovered that it was possible to enter an infinite loop if you repeatedly tried to zoom in at detail, so I have a var to prevent that in my code below.
我刚刚花了一些时间为我正在构建的应用程序进行此工作。这是我的想法:
我从 Troy Brant 的脚本开始 此页面 我认为这是设置地图视图的更好方法。
我添加了一个方法来返回当前缩放级别。
在 MKMapView+ZoomLevel.h 中:
在MKMapView+ZoomLevel.m中:
此方法依赖于上面链接的代码中的一些私有方法。
我将其添加到我的 MKMapView 委托中(如上面 @vladimir 推荐的那样)
如果用户距离太远,这会产生重新缩放的效果。您可以使用regionWillChangeAnimated 来防止地图“弹回”回来。
关于上面的循环注释,看起来这个方法只迭代一次。
I just spent some time working on this for an app i'm building. Here's what I came up with:
I started with Troy Brant's script on this page which is a nicer way to set the map view I think.
I added a method to return the current zoom level.
In MKMapView+ZoomLevel.h:
In MKMapView+ZoomLevel.m:
This method relies on a few private methods in the code linked above.
I added this in to my MKMapView delegate (as @vladimir recommended above)
This has the effect of re-zooming if the user gets too far out. You can use regionWillChangeAnimated to prevent the map from 'bouncing' back in.
Regarding the looping comments above, it looks like this method only iterates once.
是的,这是可行的。首先,使用 MKMapView+ZoomLevel 扩展 MKMapView。
然后,在您的 MKMapViewDelegate 中实现它:
Yes, this is doable. First, extend MKMapView by using MKMapView+ZoomLevel.
Then, implement this in your MKMapViewDelegate:
以下是使用 MKMapView+ZoomLevel 和 @ 在 Swift 3 中重写的代码T.Markle 回答:
在视图控制器中使用的示例:
Here is code rewritten in Swift 3 using MKMapView+ZoomLevel and @T.Markle answer:
Example of use in your view controller:
使用此示例可以锁定最大缩放范围,同样可以限制最小
map.cameraZoomRange = MKMapView.CameraZoomRange(maxCenterCooperativeDistance: 1200000)
Use this example to lock the maximum zoom range, also equally you can limit the minimum
map.cameraZoomRange = MKMapView.CameraZoomRange(maxCenterCoordinateDistance: 1200000)
如果您的目标平台是 iOS 13+,请使用 MKMapView
setCameraZoomRange
方法。只需提供最小和最大中心坐标距离(以米为单位)。请参阅此处的 Apple 文档: https://developer.apple.com/documentation/mapkit /mkmapview/3114302-setcamerazoomrange
If you are targeting iOS 13+, use the MKMapView
setCameraZoomRange
method. Simply provide the min and max center coordinate distances (measured in meters).See Apple's Documentation here: https://developer.apple.com/documentation/mapkit/mkmapview/3114302-setcamerazoomrange
不要使用
regionWillChangeAnimated
。使用regionDidChangeAnimated
我们还可以使用
setRegion(region,animated: true)
。通常,如果我们使用regionWillChangeAnimated
,它会冻结MKMapView
,但是使用regionDidChangeAnimated
它可以完美工作Don't use
regionWillChangeAnimated
. UseregionDidChangeAnimated
we can also use
setRegion(region, animated: true)
. Normally it will freezeMKMapView
if we useregionWillChangeAnimated
, but withregionDidChangeAnimated
it works perfectlyMKMapView
内部有一个MKScrollView
(私有 API),它是UIScrollView
的子类。此MKScrollView
的委托是它自己的mapView
。因此,为了控制最大缩放,请执行以下操作:
创建
MKMapView
的子类:MapView.h
MapView.m
然后,访问滚动子视图并查看
zoomScale
属性。当变焦大于某个数字时,设置最大变焦。The
MKMapView
has, inside of it, aMKScrollView
(private API), that is a subclass ofUIScrollView
. The delegate of thisMKScrollView
is its ownmapView
.So, in order to control the max zoom do the following:
Create a subclass of
MKMapView
:MapView.h
MapView.m
Then, access the scroll subview and see the
zoomScale
property. When the zoom is greater than a number, set your max zoom.Raphael Petegrosso 发布的带有扩展 MKMapView 的文章经过一些小的修改后效果很好。
下面的版本也更加“用户友好”,因为一旦用户放开屏幕,它就会优雅地“快速”回到定义的缩放级别,与苹果自己的弹性滚动感觉类似。
编辑:这个解决方案不是最佳的,会破坏/损坏地图视图,我在这里找到了一个更好的解决方案:如何检测MKMapView 内的任何点击。这允许您拦截捏合和其他动作。
MyMapView.h
MyMapView.m
对于硬限制,请使用以下内容:
The post by Raphael Petegrosso with the extended MKMapView works great with some small modifications.
The version below is also much more "user friendly", as it gracefully "snaps" back to the defined zoom level as soon as the user lets go of the screen, being similar in feel to Apple's own bouncy scrolling.
Edit: This solution is not optimal and will break/damage the map view, I found a much better solution here: How to detect any tap inside an MKMapView. This allows you to intercept pinching and other motions.
MyMapView.h
MyMapView.m
For a hard limit, use this:
以下代码对我有用,并且在概念上易于使用,因为它根据以米为单位的距离设置区域。
该代码源自@nevan-king 发布的答案和@Awais-Fayyaz 发布的使用regionDidChangeAnimated 的评论
将以下扩展添加到您的MapViewDelegate
然后为MKCooperativeRegion 定义一个扩展,如下所示。
上面的 MKCooperativeRegion 代码片段是由@Gerd-Castan 在这个问题上发布的:
MKCooperativeRegionMakeWithDistance 的反向函数?
The following code worked for me and is conceptually easy to use because it sets the region based on a distance in meters.
The code is derived from the answer posted by: @nevan-king and the comment posted by @Awais-Fayyaz to use regionDidChangeAnimated
Add the following extension to your MapViewDelegate
Then define an extension for MKCoordinateRegion as follows.
The above snippet for MKCoordinateRegion was posted by @Gerd-Castan on this question:
Reverse function of MKCoordinateRegionMakeWithDistance?
我在工作中遇到了这个问题,并且创建了一些效果相当好的东西,而无需设置全局限制。
我利用的 MapView 委托是:
-mapViewDidFinishRendering
- mapViewRegionDidChange
我的解决方案背后的前提是,由于卫星视图呈现没有数据的区域,因此它始终是同一件事。这个可怕的图像(https://i.sstatic.net/7Guev.jpg)如果我们可以轻松地依赖该失败案例,我们可以将其用作确定用户所看到内容的关键。地图渲染后,我截取渲染地图边界的屏幕截图并确定平均 RGB 值。根据该 RGB 值,我假设该区域没有数据。如果是这种情况,我会将地图弹出到正确渲染的最后一个跨度。
我唯一的全局检查是当它开始检查地图时,您可以根据需要增加或减少该设置。下面是完成此任务的原始代码,并将构建一个示例项目以供贡献。您可以提供的任何优化将不胜感激,并希望它有所帮助。
I've run into this very issue at work and have created something that works fairly well without setting a global limit.
The MapView delegates that I leverage are:
- mapViewDidFinishRendering
- mapViewRegionDidChange
The premise behind my solution is that since a satellite view renders an area with no data it is always the same thing. This dreaded image (https://i.sstatic.net/7Guev.jpg) If we can comfortably rely on that fail case we can use it as a key for determining wha the user is seeing. After the map renders, I take a screenshot of the rendered map bounds and determing an average RGB value. Based off of that RGB value, I assume that the area in question has no data. If that's the case I pop the map back out to the last span that was rendered correctly.
The only global check I have is when it starts to check the map, you can increase or decrease that setting based on your needs. Below is the raw code that will accomplish this and will be putting together a sample project for contribution. Any optimizations you can offer would be appreciated and hope it helps.