我正在尝试做两个MKCoordinateRegion之间的联合。有人对如何执行此操作有想法吗?

最佳答案

有一个MKMapRectUnion函数可以接受两个MKMapRects,因此您可以先将每个MKCoordinateRegion转换为MKMapRect,然后调用该函数(然后使用MKCoordinateRegion函数将结果转换回MKCoordinateRegionForMapRect)。

转换方法可能如下所示:

- (MKMapRect)mapRectForCoordinateRegion:(MKCoordinateRegion)coordinateRegion
{
    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));

    return mapRect;
}

然后,实际进行合并:
MKCoordinateRegion region1 = ...
MKCoordinateRegion region2 = ...

MKMapRect mapRect1 = [self mapRectForCoordinateRegion:region1];
MKMapRect mapRect2 = [self mapRectForCoordinateRegion:region2];

MKMapRect mapRectUnion = MKMapRectUnion(mapRect1, mapRect2);

MKCoordinateRegion regionUnion = MKCoordinateRegionForMapRect(mapRectUnion);

10-08 05:30