将 MKMapView 区域设置为以两个注释为中心
我正在寻求一些帮助来完成一些代码,以根据当前位置注释和我设置的注释在 MKMapView 上设置区域。
我想计算两者之间的距离并设置两者之间的中心,然后缩小以便两者都在视图中。对我来说,它在模拟器中似乎运行良好,但不幸的是 userLocation.coordinate 被固定为 Apple HQ。当我在设备上进行测试时,我看到了奇怪的行为。如果两个注释在同一纬度上有些水平,它通常会缩小并设置适当的区域,但如果垂直距离较大,则无法正确缩小。
我使用了此处找到的代码,并进行了一些编辑以满足我的需求:让
CLLocationCoordinate2D southWest = mapView.userLocation.coordinate;
CLLocationCoordinate2D northEast = southWest;
southWest.latitude = MIN(southWest.latitude, annotation.coordinate.latitude);
southWest.longitude = MIN(southWest.longitude, annotation.coordinate.longitude);
northEast.latitude = MAX(northEast.latitude, annotation.coordinate.latitude);
northEast.longitude = MAX(northEast.longitude, annotation.coordinate.longitude);
CLLocation *locSouthWest = [[CLLocation alloc] initWithLatitude:southWest.latitude longitude:southWest.longitude];
CLLocation *locNorthEast = [[CLLocation alloc] initWithLatitude:northEast.latitude longitude:northEast.longitude];
// This is a diag distance (if you wanted tighter you could do NE-NW or NE-SE)
CLLocationDistance meters = [locSouthWest distanceFromLocation:locNorthEast];
MKCoordinateRegion region;
region.center.latitude = (southWest.latitude + northEast.latitude) / 2.0;
region.center.longitude = (southWest.longitude + northEast.longitude) / 2.0;
region.span.latitudeDelta = meters / 111319.5;
region.span.longitudeDelta = 0.0;
MKCoordinateRegion savedRegion = [mapView regionThatFits:region];
[mapView setRegion:savedRegion animated:YES];
[locSouthWest release];
[locNorthEast release];
我困惑的一件事是他说northEast = SouthWest
...
提前感谢任何获得帮助和意见的人:)
I'm looking for some help with finishing some code on setting the region on a MKMapView based on the current location annotation and an annotation I have set.
I want to calculate the distance between the two and set the center between the two and then zoom out so both are in view. It appears to work fine in Simulator for me, but unfortunately the userLocation.coordinate is fixed to Apple HQ. When I test on a device, I am seeing weird behaviour. Often it'll zoom out and set a proper region if the two annotations are somewhat horizontally on the same latitude, but if the vertical distance is greater it's not zooming out properly.
I have used code found here, and edited a little bit to fit my needs:
CLLocationCoordinate2D southWest = mapView.userLocation.coordinate;
CLLocationCoordinate2D northEast = southWest;
southWest.latitude = MIN(southWest.latitude, annotation.coordinate.latitude);
southWest.longitude = MIN(southWest.longitude, annotation.coordinate.longitude);
northEast.latitude = MAX(northEast.latitude, annotation.coordinate.latitude);
northEast.longitude = MAX(northEast.longitude, annotation.coordinate.longitude);
CLLocation *locSouthWest = [[CLLocation alloc] initWithLatitude:southWest.latitude longitude:southWest.longitude];
CLLocation *locNorthEast = [[CLLocation alloc] initWithLatitude:northEast.latitude longitude:northEast.longitude];
// This is a diag distance (if you wanted tighter you could do NE-NW or NE-SE)
CLLocationDistance meters = [locSouthWest distanceFromLocation:locNorthEast];
MKCoordinateRegion region;
region.center.latitude = (southWest.latitude + northEast.latitude) / 2.0;
region.center.longitude = (southWest.longitude + northEast.longitude) / 2.0;
region.span.latitudeDelta = meters / 111319.5;
region.span.longitudeDelta = 0.0;
MKCoordinateRegion savedRegion = [mapView regionThatFits:region];
[mapView setRegion:savedRegion animated:YES];
[locSouthWest release];
[locNorthEast release];
One thing that confused me is that he says northEast = southWest
...
Thanks in advance to anyone who's got some help and input :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这篇博客文章可能会为您提供所需的见解
http://codisllc.com /blog/zoom-mkmapview-to-fit-annotations/
this blog posting might provide you with the insight to need
http://codisllc.com/blog/zoom-mkmapview-to-fit-annotations/
不要被前两行吓到,你可以忽略 = 符号的正确内容,因为它会在下面被覆盖......
我认为问题出在这里:
Don't get scared by the two first lines, you can ignore what's right of = signs, as it will be overwritten below...
I think the issue is here :
Swift 4 版本的 Robertibiris 答案:
Swift 4 version of Robertibiris answer:
对于 iOS7 向前版本,最好的方法是:
对于我的个人项目(iOS7 之前的版本),我只是在 MKMapView 类上添加了一个类别来封装一个非常常见的操作的“可见区域”功能:将其设置为能够看到MKMapView 实例上当前加载的所有注释(这包括您可能放置的尽可能多的图钉,以及用户的位置)。结果是这样的:
.h 文件
.m 文件
如您所见,到目前为止我添加了 2 种方法:一种用于将地图的可见区域设置为适合 MKMapView 实例上当前加载的所有注释的区域,以及另一种将其设置为任何对象数组的方法。
因此,要设置 mapView 的可见区域,代码将非常简单:
我希望它有帮助 =)
For iOS7 forward the best way to do this is:
For my personal project (prior to iOS7) I simply added a category on the MKMapView class to encapsulate the "visible area" functionality for a very common operation: setting it to be able to see all the currently-loaded annotations on the MKMapView instance (this includes as many pins as you might have placed, as well as the user's location). 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 =)