Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
538 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...