将MKCoordinateRegion转换为MKMapRect

Jon*_*Cox 36 mkcoordinateregion mkmapview ios mkmaprect

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

创建MKCoordinateRegion并将地图设置为它(如此代码中所示...

MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(center_coord, 1000.0, 1000.0);
[self.mapView setRegion:region animated:YES];
Run Code Online (Sandbox Code Playgroud)

..)无法正常工作,因为在这里使用区域只意味着至少显示该区域,通常比区域更多.


我打算使用setVisibleMapRect:animated:方法,因为我相信这会缩放到实际传递的MKMapRect.

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

我在Map Kit函数参考中看不到任何方便的东西.

(使用iOS 5,Xcode 4.2)

leo*_*leo 55

要向堆中添加另一个实现:

- (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));
}
Run Code Online (Sandbox Code Playgroud)

注意:有许多方法可以在MKMapRect和之间进行转换MKCoordinateRegion.这个当然不是完全相反的MKCoordinateRegionMakeWithDistance(),但相当接近它.所以,要小心来回转换,因为信息可能会丢失.

  • 谢谢!救了我一些时间. (2认同)
  • @PavelGurov 是对的,尽管在某些情况下有效,例如接近最大/最小。经度它会产生错误的结果。 (2认同)

Mik*_*ike 27

这是Leo&Barnhart解决方案的Swift版本

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)))
}
Run Code Online (Sandbox Code Playgroud)

  • 对于 Swift 4.2 更改此: let a = MKMapPoint(topLeft) let b = MKMapPoint(bottomRight) (4认同)

Bog*_*dan 12

使用MKMapPointForCoordinate转换区域的2个点(顶部/左侧和底部/右侧),然后使用2个MKMapPoints创建MKMapRect

        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);
Run Code Online (Sandbox Code Playgroud)


cha*_*tur 10

你可以使用方法转换MKCoordinateRegionCGRect

- (CGRect)convertRegion:(MKCoordinateRegion)region toRectToView:(UIView *)view
Run Code Online (Sandbox Code Playgroud)

并使用 - (MKMapRect)mapRectForRect:(CGRect)rect

或者使用MKMapPointForCoordinate方法首先将坐标转换为MKPoint并使用它来形成MKMapRect最终使用setVisibleMapRect:animated:

  • 你能添加你提到的函数所属的类吗?可能还有一个关于如何实际进行转换的完整示例 (2认同)
  • @Daniel它在iOS7中被弃用了,这可能是你找不到这个方法的原因.您可以在overlay类中找到它. (2认同)

Dav*_*vid 9

@Bogdan

我认为它应该是:

 CLLocationCoordinate2D topLeftCoordinate =
CLLocationCoordinate2DMake(coordinateRegion.center.latitude
                           + (coordinateRegion.span.latitudeDelta/2.0),
                           coordinateRegion.center.longitude
                           - (coordinateRegion.span.longitudeDelta/2.0));

MKMapPoint topLeftMapPoint = MKMapPointForCoordinate(topLeftCoordinate);

CLLocationCoordinate2D bottomRightCoordinate =
CLLocationCoordinate2DMake(coordinateRegion.center.latitude
                           - (coordinateRegion.span.latitudeDelta/2.0),
                           coordinateRegion.center.longitude
                           + (coordinateRegion.span.longitudeDelta/2.0));

MKMapPoint bottomRightMapPoint = MKMapPointForCoordinate(bottomRightCoordinate);

MKMapRect mapRect = MKMapRectMake(topLeftMapPoint.x,
                                  topLeftMapPoint.y,
                                  fabs(bottomRightMapPoint.x-topLeftMapPoint.x),
                                  fabs(bottomRightMapPoint.y-topLeftMapPoint.y));
Run Code Online (Sandbox Code Playgroud)

根据apple api的参考,MKCoordinateRegion.center代表该地区的中心点; 和MKCoordinateSpan.latitudeDelta表示要在地图上显示的北 - 南距离(以度为单位)的数量; MKCoordinateSpan.longitudeDelta表示要为地图区域显示的东西距离(以度为单位)的数量.