如何在两个 MKCooperativeRegion 之间建立并集

发布于 2024-11-25 10:36:48 字数 56 浏览 3 评论 0原文

我正在尝试在两个 MKCooperativeRegion 之间进行并集。有人知道如何做到这一点吗?

I'm trying to do the union between two MKCoordinateRegion. Does anybody have an idea on how to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夏末染殇 2024-12-02 10:36:48

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

转换方法可能如下所示:

- (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);

There is a MKMapRectUnion function which accepts two MKMapRects so you could first convert each MKCoordinateRegion to an MKMapRect and then call that function (and convert the result back to an MKCoordinateRegion using the MKCoordinateRegionForMapRect function).

The conversion method might look like this:

- (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;
}

Then, to actually do the union:

MKCoordinateRegion region1 = ...
MKCoordinateRegion region2 = ...

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

MKMapRect mapRectUnion = MKMapRectUnion(mapRect1, mapRect2);

MKCoordinateRegion regionUnion = MKCoordinateRegionForMapRect(mapRectUnion);
人生戏 2024-12-02 10:36:48

如果有人正在寻找使用扩展的快速等效项,

extension MKCoordinateRegion {
    func mapRectForCoordinateRegion() -> MKMapRect {
        let dLat = span.latitudeDelta / 2.0
        let dLon = span.longitudeDelta / 2.0

        let topLeft = MKMapPointForCoordinate(
            CLLocationCoordinate2DMake(center.latitude + dLat, center.longitude - dLon))
        let botRight = MKMapPointForCoordinate(
            CLLocationCoordinate2DMake(center.latitude - dLat, center.longitude + dLon))

        return MKMapRectMake(
            topLeft.x,
            topLeft.y,
            fabs(botRight.x - topLeft.x),
            fabs(botRight.y - topLeft.y))
    }

    func unionWith(region: MKCoordinateRegion) -> MKCoordinateRegion {
        let union = MKMapRectUnion(
            self.mapRectForCoordinateRegion(),
            region.mapRectForCoordinateRegion())

        return MKCoordinateRegionForMapRect(union)
    }
}

并且可以像这样使用

let region1 = ...
let region2 = ...

let regionUnion = region1.unionWith(region2)

If anyone is looking for a swift equivalent using extensions,

extension MKCoordinateRegion {
    func mapRectForCoordinateRegion() -> MKMapRect {
        let dLat = span.latitudeDelta / 2.0
        let dLon = span.longitudeDelta / 2.0

        let topLeft = MKMapPointForCoordinate(
            CLLocationCoordinate2DMake(center.latitude + dLat, center.longitude - dLon))
        let botRight = MKMapPointForCoordinate(
            CLLocationCoordinate2DMake(center.latitude - dLat, center.longitude + dLon))

        return MKMapRectMake(
            topLeft.x,
            topLeft.y,
            fabs(botRight.x - topLeft.x),
            fabs(botRight.y - topLeft.y))
    }

    func unionWith(region: MKCoordinateRegion) -> MKCoordinateRegion {
        let union = MKMapRectUnion(
            self.mapRectForCoordinateRegion(),
            region.mapRectForCoordinateRegion())

        return MKCoordinateRegionForMapRect(union)
    }
}

And can be used like

let region1 = ...
let region2 = ...

let regionUnion = region1.unionWith(region2)
最笨的告白 2024-12-02 10:36:48

我在 4.3 和 5.0 上做了一些测试,似乎转换不正确

UIImageView *imageView = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"heart.png"] imageByScalingProportionallyToSize:CGSizeMake(100, 100)]];
imageView.frame = CGRectMake(0, 0, 100, 100);
imageView.center = [mapView convertCoordinate:mapView.centerCoordinate toPointToView:canvasView];
[canvasView addSubview:imageView];

MKCoordinateRegion region = [mapView convertRect:imageView.frame toRegionFromView:canvasView];
MKMapRect maprect;
for (int i = 0; i < 5; i ++)
{
    maprect = [mapView mapRectForCoordinateRegion:region];
    NSLog(@"%f, %f, %f, %f", region.center.latitude, region.center.longitude, region.span.latitudeDelta, region.span.longitudeDelta);
    NSLog(@"%f, %f, %f, %f", maprect.origin.x, maprect.origin.y, maprect.size.width, maprect.size.height);
    region = MKCoordinateRegionForMapRect([mapView mapRectForCoordinateRegion:region]);
}

14.718256、-40.078125、63.837150、70.312500

78118912.000000、94811514.406252、52428800.000000、52428799.999997

17.416993、-40.078125、63.837150、70.312500

78118912.000000、91803983.982924、52428800.000000、53344239.567355

20.649080、-40.078125、63.837150、70.312500

78118912.000000、87976363.978412、52428800.000000、54699220.734715

24.548345、-40.078125、63.837150、70.312500

78118912.000000、82962597.189840、52428800.000000、56765991.576236

29.305644、-40.078125、63.837150、70.312500

78118912.000000、76093086.024249、52428800.000000、60073659.997575

i did a little test on both 4.3 and 5.0 and it seems that the conversion is not correct

UIImageView *imageView = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"heart.png"] imageByScalingProportionallyToSize:CGSizeMake(100, 100)]];
imageView.frame = CGRectMake(0, 0, 100, 100);
imageView.center = [mapView convertCoordinate:mapView.centerCoordinate toPointToView:canvasView];
[canvasView addSubview:imageView];

MKCoordinateRegion region = [mapView convertRect:imageView.frame toRegionFromView:canvasView];
MKMapRect maprect;
for (int i = 0; i < 5; i ++)
{
    maprect = [mapView mapRectForCoordinateRegion:region];
    NSLog(@"%f, %f, %f, %f", region.center.latitude, region.center.longitude, region.span.latitudeDelta, region.span.longitudeDelta);
    NSLog(@"%f, %f, %f, %f", maprect.origin.x, maprect.origin.y, maprect.size.width, maprect.size.height);
    region = MKCoordinateRegionForMapRect([mapView mapRectForCoordinateRegion:region]);
}

14.718256, -40.078125, 63.837150, 70.312500

78118912.000000, 94811514.406252, 52428800.000000, 52428799.999997

17.416993, -40.078125, 63.837150, 70.312500

78118912.000000, 91803983.982924, 52428800.000000, 53344239.567355

20.649080, -40.078125, 63.837150, 70.312500

78118912.000000, 87976363.978412, 52428800.000000, 54699220.734715

24.548345, -40.078125, 63.837150, 70.312500

78118912.000000, 82962597.189840, 52428800.000000, 56765991.576236

29.305644, -40.078125, 63.837150, 70.312500

78118912.000000, 76093086.024249, 52428800.000000, 60073659.997575

人生戏 2024-12-02 10:36:48
//usage >> let regionMerged = [region1, region2,......, regionN].unionRegion

internal extension MKCoordinateRegion {
    var northDegree: CLLocationDegrees { center.latitude + (span.latitudeDelta  / 2) }
    var southDegree: CLLocationDegrees { center.latitude - (span.latitudeDelta  / 2) }
    var eastDegree: CLLocationDegrees { center.longitude + (span.longitudeDelta / 2) }
    var westDegree: CLLocationDegrees { center.longitude - (span.longitudeDelta / 2) }

    var northWestPoint: CLLocationCoordinate2D { CLLocationCoordinate2D(latitude: northDegree, longitude: westDegree) }
    var southEastPoint: CLLocationCoordinate2D { CLLocationCoordinate2D(latitude: southDegree, longitude: eastDegree) }

    var mapRect: MKMapRect {
        let topLeft = northWestPoint
        let bottomRight = southEastPoint

        let a = MKMapPoint(topLeft)
        let b = MKMapPoint(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)))
    }

    static func + (lhs: MKCoordinateRegion, rhs: MKCoordinateRegion) -> MKCoordinateRegion {
        let union = lhs.mapRect + rhs.mapRect
        return union.coordinateRegion
    }
}

internal extension [MKCoordinateRegion] {
    var unionRegion: MKCoordinateRegion {
        self.reduce(MKCoordinateRegion() , +)
    }
}

internal extension MKMapRect {
    static func + (lhs: MKMapRect, rhs: MKMapRect) -> MKMapRect {
        lhs.union(rhs)
    }

    var coordinateRegion: MKCoordinateRegion {
        MKCoordinateRegion(self)
    }
}
//usage >> let regionMerged = [region1, region2,......, regionN].unionRegion

internal extension MKCoordinateRegion {
    var northDegree: CLLocationDegrees { center.latitude + (span.latitudeDelta  / 2) }
    var southDegree: CLLocationDegrees { center.latitude - (span.latitudeDelta  / 2) }
    var eastDegree: CLLocationDegrees { center.longitude + (span.longitudeDelta / 2) }
    var westDegree: CLLocationDegrees { center.longitude - (span.longitudeDelta / 2) }

    var northWestPoint: CLLocationCoordinate2D { CLLocationCoordinate2D(latitude: northDegree, longitude: westDegree) }
    var southEastPoint: CLLocationCoordinate2D { CLLocationCoordinate2D(latitude: southDegree, longitude: eastDegree) }

    var mapRect: MKMapRect {
        let topLeft = northWestPoint
        let bottomRight = southEastPoint

        let a = MKMapPoint(topLeft)
        let b = MKMapPoint(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)))
    }

    static func + (lhs: MKCoordinateRegion, rhs: MKCoordinateRegion) -> MKCoordinateRegion {
        let union = lhs.mapRect + rhs.mapRect
        return union.coordinateRegion
    }
}

internal extension [MKCoordinateRegion] {
    var unionRegion: MKCoordinateRegion {
        self.reduce(MKCoordinateRegion() , +)
    }
}

internal extension MKMapRect {
    static func + (lhs: MKMapRect, rhs: MKMapRect) -> MKMapRect {
        lhs.union(rhs)
    }

    var coordinateRegion: MKCoordinateRegion {
        MKCoordinateRegion(self)
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文