如何在两个mkcoordinaterregion之间进行并集



我试图做两个mkcoordinaterregion之间的联合。有人知道怎么做吗?

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

转换方法可能如下所示:

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

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

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可以像

一样使用
let region1 = ...
let region2 = ...
let regionUnion = region1.unionWith(region2)

我在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
78118912000000, 91803983.982924, 52428800.000000, 53344239.567355
20.649080, -40.078125, 63.837150, 70.312500
78118912000000, 87976363.978412, 52428800.000000, 54699220.734715
24.548345, -40.078125, 63.837150, 70.312500
78118912000000, 82962597.189840, 52428800.000000, 56765991.576236
29.305644, -40.078125, 63.837150, 70.312500
78118912000000, 76093086.024249, 52428800.000000, 60073659.997575

相关内容

  • 没有找到相关文章

最新更新