首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将MKCoordinateRegion转换为MKMapRect

将MKCoordinateRegion转换为MKMapRect
EN

Stack Overflow用户
提问于 2012-02-14 00:50:17
回答 11查看 35.6K关注 0票数 45

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

创建一个MKCoordinateRegion并将映射设置为它(如下面的代码所示)。

代码语言:javascript
复制
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(center_coord, 1000.0, 1000.0);
[self.mapView setRegion:region animated:YES];

..)不能正常工作,因为在这里使用区域只意味着至少会显示该区域,通常比区域显示的要多。

我计划使用setVisibleMapRect:动画:方法,因为我相信这将放大到实际通过的MKMapRect

那么,有一种简单的方法可以在和MKMapRect之间进行转换吗?可能获取该区域的左上角和右下角坐标,并使用它们来生成MKMapRect?

我在地图工具包函数参考里看不到任何有用的东西。

(使用iOS 5,Xcode 4.2)

EN

回答 11

Stack Overflow用户

回答已采纳

发布于 2013-03-28 13:22:51

若要向堆中添加另一个实现,请执行以下操作:

代码语言:javascript
复制
- (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));
}

注:在MKMapRectMKCoordinateRegion之间有很多种转换方式。这个当然不是MKCoordinateRegionMakeWithDistance()的精确逆,但它相当好地逼近它。所以,要小心来回转换,因为信息可能会丢失。

票数 61
EN

Stack Overflow用户

发布于 2016-02-10 17:11:11

这是Leo & Barnhart解决方案的一个快速版本。

代码语言:javascript
复制
func MKMapRectForCoordinateRegion(region:MKCoordinateRegion) -> MKMapRect {
    let topLeft = CLLocationCoordinate2D(latitude: region.center.latitude + (region.span.latitudeDelta/2), longitude: region.center.longitude - (region.span.longitudeDelta/2))
    let bottomRight = CLLocationCoordinate2D(latitude: region.center.latitude - (region.span.latitudeDelta/2), longitude: region.center.longitude + (region.span.longitudeDelta/2))

    let a = MKMapPointForCoordinate(topLeft)
    let b = MKMapPointForCoordinate(bottomRight)
    
    return MKMapRect(origin: MKMapPoint(x:min(a.x,b.x), y:min(a.y,b.y)), size: MKMapSize(width: abs(a.x-b.x), height: abs(a.y-b.y)))
}
票数 43
EN

Stack Overflow用户

发布于 2012-06-21 18:14:55

使用MKMapPointForCoordinate转换区域的2点(上/左和下/右),然后使用2 MKMapPoints创建MKMapRect

代码语言:javascript
复制
        CLLocationCoordinate2D coordinateOrigin = CLLocationCoordinate2DMake(latitude, longitude);
        CLLocationCoordinate2D coordinateMax = CLLocationCoordinate2DMake(latitude + cellSize, longitude + cellSize);

        MKMapPoint upperLeft = MKMapPointForCoordinate(coordinateOrigin);
        MKMapPoint lowerRight = MKMapPointForCoordinate(coordinateMax);

        MKMapRect mapRect = MKMapRectMake(upperLeft.x,
                                          upperLeft.y,
                                          lowerRight.x - upperLeft.x,
                                          lowerRight.y - upperLeft.y);
票数 12
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9270268

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档