将MKCoordinateRegion转换为MKMapRect [英] Convert MKCoordinateRegion to MKMapRect

查看:508
本文介绍了将MKCoordinateRegion转换为MKMapRect的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有一个正方形MKMapView,我希望以米为单位设置一个中心点和视图的确切高度/宽度。

I have a square MKMapView in my app, and I wish to set a center point and the exact height/width of the view in meters.

创建一个MKCoordinateRegion并将地图设置为它(如此代码...

Creating an MKCoordinateRegion and setting the map to it (as in this code...

MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(center_coord, 1000.0, 1000.0);
[self.mapView setRegion:region animated:YES];

..)doesn'正常工作,因为在这里使用区域只意味着至少显示该区域,通常超过该区域。

..) doesn't work properly because using regions here just means that at least that region is displayed, typically more than the region is.



我是计划使用 setVisibleMapRect:animated:方法,我相信这会缩放到实际的 MKMapRect 已通过。


I'm planning on using setVisibleMapRect:animated: method instead, as I believe this will zoom to the actual MKMapRect passed.

那么,是否有在MKcoordinateRegion和MKMapRect之间转换的简单方法?也许获得该区域的左上角和右下角坐标,并使用它们来制作MKMapRect?

我看不到任何方便的东西在地图套件功能参考

I couldn't see anything handy in the Map Kit Functions Reference.

(使用iOS 5,Xcode 4.2)

推荐答案

要添加另一个实现:

- (MKMapRect)MKMapRectForCoordinateRegion:(MKCoordinateRegion)region
{
    MKMapPoint a = MKMapPointForCoordinate(CLLocationCoordinate2DMake(
        region.center.latitude + region.span.latitudeDelta / 2, 
        region.center.longitude - region.span.longitudeDelta / 2));
    MKMapPoint b = MKMapPointForCoordinate(CLLocationCoordinate2DMake(
        region.center.latitude - region.span.latitudeDelta / 2, 
        region.center.longitude + region.span.longitudeDelta / 2));
    return MKMapRectMake(MIN(a.x,b.x), MIN(a.y,b.y), ABS(a.x-b.x), ABS(a.y-b.y));
}

注意:有很多方法可以在 MKMapRect之间进行转换 MKCoordinateRegion 。这个肯定是 MKCoordinateRegionMakeWithDistance()的确切倒数,但是相当接近它。所以,要小心来回转换,因为信息可能会丢失。

NB: There are many ways to convert between MKMapRect and MKCoordinateRegion. This one certainly is not the exact inverse of MKCoordinateRegionMakeWithDistance(), but approximates it fairly well. So, be careful converting back and forth, because information can be lost.

这篇关于将MKCoordinateRegion转换为MKMapRect的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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