定位 MKMapView 以一次显示多个注释
我有几个注释想要添加到我的 MKMapView 中(它可以是 0-n 个项目,其中 n 通常约为 5)。我可以很好地添加注释,但我想调整地图大小以一次适应屏幕上的所有注释,并且我不确定如何执行此操作。
我一直在查看 -regionThatFits:
但我不太确定如何处理它。我将发布一些代码来展示到目前为止我所得到的。我认为这应该是一项简单的任务,但到目前为止我对 MapKit 感到有点不知所措。
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
location = newLocation.coordinate;
//One location is obtained.. just zoom to that location
MKCoordinateRegion region;
region.center = location;
//Set Zoom level using Span
MKCoordinateSpan span;
span.latitudeDelta = 0.015;
span.longitudeDelta = 0.015;
region.span = span;
// Set the region here... but I want this to be a dynamic size
// Obviously this should be set after I've added my annotations
[mapView setRegion:region animated:YES];
// Test data, using these as annotations for now
NSArray *arr = [NSArray arrayWithObjects:@"one", @"two", @"three", @"four", nil];
float ex = 0.01;
for (NSString *s in arr) {
JBAnnotation *placemark = [[JBAnnotation alloc] initWithLat:(location.latitude + ex) lon:location.longitude];
[mapView addAnnotation:placemark];
ex = ex + 0.005;
}
// What do I do here?
[mapView setRegion:[mapView regionThatFits:region] animated:YES];
}
请注意,这一切都是在我收到位置更新时发生的……我不知道这是否是执行此操作的合适位置。如果没有,哪里会是更好的地方? -viewDidLoad
?
I've got several annotations I want to add to my MKMapView (it could 0-n items, where n is generally around 5). I can add the annotations fine, but I want to resize the map to fit all annotations onscreen at once, and I'm not sure how to do this.
I've been looking at -regionThatFits:
but I'm not quite sure what to do with it. I'll post some code to show what I've got so far. I think this should be a generally straightforward task but I'm feeling a bit overwhelmed with MapKit so far.
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
location = newLocation.coordinate;
//One location is obtained.. just zoom to that location
MKCoordinateRegion region;
region.center = location;
//Set Zoom level using Span
MKCoordinateSpan span;
span.latitudeDelta = 0.015;
span.longitudeDelta = 0.015;
region.span = span;
// Set the region here... but I want this to be a dynamic size
// Obviously this should be set after I've added my annotations
[mapView setRegion:region animated:YES];
// Test data, using these as annotations for now
NSArray *arr = [NSArray arrayWithObjects:@"one", @"two", @"three", @"four", nil];
float ex = 0.01;
for (NSString *s in arr) {
JBAnnotation *placemark = [[JBAnnotation alloc] initWithLat:(location.latitude + ex) lon:location.longitude];
[mapView addAnnotation:placemark];
ex = ex + 0.005;
}
// What do I do here?
[mapView setRegion:[mapView regionThatFits:region] animated:YES];
}
Notice, this all happens as I receive a location update... I don't know if that's an appropriate place to do this. If not, where would be a better place? -viewDidLoad
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(24)
为什么这么复杂?
Why so complicated?
Jim 发布的链接现已失效,但我能够找到代码(我已在某处添加了书签)。
The link posted by Jim is now dead, but I was able to find the code (which I had bookmarked somewhere).
从 iOS7 开始,您可以使用 显示注释:动画:
As of iOS7 you can use showAnnotations:animated:
我已经做了与此类似的操作,以缩小(或缩小)到包含点注释和当前位置的区域。您可以通过循环注释来扩展它。
基本步骤是:
点与距离之间的换算
以度数
I have done something similiar to this to zoom out (or in) to an area that included a point annotation and the current location. You could expand this by looping through your annotations.
The basic steps are:
between points and distance converted
to degrees
我有不同的答案。我本来打算自己实现缩放到适合算法,但我认为苹果必须有办法做到我们想要的,而不需要做太多工作。
使用 API doco 很快就表明我可以使用 MKPolygon 来完成所需的操作:
希望这会有所帮助。
I have a different answer. I was going to do implement the zoom-to-fit algorithm myself, but i figured that Apple must have a way to do what we wanted without so much work.
Using the API doco quickly showed that I could use MKPolygon to do what was needed:
Hope this helps.
你也可以这样做..
you can also do it this way..
根据大家提供的信息和建议,我得出以下结论。感谢本次讨论中每个人的贡献:) 这将出现在包含 mapView 的视图控制器中。
Based on the information and suggestions from everyone I came up with the following. Thanks for everyone in this discussion for contributing :) This would go in the view Controller that contains the mapView.
使用 Swift、多边形和一些额外的填充,我使用了以下内容:
Using Swift, a polygon, and some extra padding I used the following:
就我而言,我从 CLLocation 对象开始,并为每个对象创建注释。
我只需要放置两个注释,因此我有一种简单的方法来构建点数组,但它可以轻松扩展以构建一个给定一组 CLLocation 的任意长度的数组。
这是我的实现(不需要创建 MKMapPoints):
In my case, I am starting with CLLocation objects and creating annotations for each of them.
I only need to place two annotations, so I have a simple approach to building the array of points, but it could easily be expanded to build an array with an arbitrary length given a set of CLLocations.
Here's my implementation (doesn't require creating MKMapPoints):
我知道这是一个老问题,但是,如果您想显示地图上已存在的所有注释,请使用以下命令:
I know this is an old question but, if you want to display all the annotations ALREADY ON the map use this:
以下是 Mustafa 的答案的 SWIFT 等效项(已确认工作于:Xcode6.1、SDK 8.2):
Here is the SWIFT Equivalent (Confirmed Working in : Xcode6.1, SDK 8.2) for Mustafa's Answers:
从 iOS 7 开始,“MKMapView”中有一个新方法可供您使用
There's a new method in 'MKMapView' as of iOS 7 that you can use
对Stéphane de Luca的答案进行补充。这里我们可以保留 UserLocation 加上自定义注释以适应 MKMapView
Adding additional to Stéphane de Luca's answer. Here we can keep UserLocation plus Custom annotations to fit MKMapView
基于
me2
的出色回答(现在在 Swift 中)Based on the excellent answer by
me2
(now in Swift)一种可能的解决方案可能是测量当前位置与所有注释之间的距离,并使用 MKCooperativeRegionMakeWithDistance 方法创建一个距离比最远注释稍大的区域。
当然,您添加的注释越多,速度就会越慢。
One possible solution might be measuring the distance between the current location and all the annotations and using the MKCoordinateRegionMakeWithDistance method to make a region that has a slightly greater distance than the furthest annotation.
This would of course get slower the more annotations you added though.
添加了一些 if 子句来处理 1 个位置 - 添加到 Mustufa 的 cound 代码片段。为此使用 pkclSoft 的 ZoomToAnnotation 函数:
Added a little if clause to handle 1 location- to add to mustufa's cound code snippet. Used pkclSoft's zoomToAnnotation function for that:
这段代码对我有用,它显示了当前位置的所有引脚,希望这对您有帮助,
this code works for me, it shows all the pins with current location, hope this helps you,
我希望这至少是相关的,这是我为 Mono 整理的内容(基于 pkclSoft 的答案):
I hope this is at least relevant, this is what I put together for Mono (based off of pkclSoft's answer):
基于 me2 响应,我为 MKMapView 编写了一个类别,以添加一些边距并跳过用户位置注释:
Based on me2 response I wrote a category for MKMapView to add some margins and skip user location annotation:
由于我无法对答案发表评论,因此我想在 @me2 的答案中添加一点便利(因为我认为这是在这里找到的最优雅的方法)。
对于我的个人项目,我只是在 MKMapView 类上添加了一个类别,以封装非常常见的操作的“可见区域”功能:设置为能够查看 MKMapView 实例上当前加载的所有注释。结果是这样的:
.h 文件
.m 文件
如您所见,到目前为止我添加了 2 种方法:一种用于将地图的可见区域设置为适合 MKMapView 实例上当前加载的所有注释的区域,以及另一种将其设置为任何对象数组的方法。
因此,要设置 mapView 的可见区域,代码将非常简单:
我希望它有帮助 =)
Since I can't comment on an answer, I would like to add my bit of convenience into @me2 's answer (since i thought it was the most elegant approach Found here).
For my personal project I simply added a category on the MKMapView class to encapsulate the "visible area" functionality for a ver common operation: setting to be able to see all the currently-loaded annotations on the MKMapView instance. the result was this:
.h file
.m file
As you can see, I've added 2 methods so far: one for setting the visible region of the map to the one that fits all currently-loaded annotations on the MKMapView instance, and another method to set it to any array of objects.
So to set the mapView's visible region the code would then be as simple as:
I hope it helps =)
考虑这个扩展:
Consider this extension:
Swift 5 版本:
A swift 5 version: