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:osI'm trying to do the union between two MKCoordinateRegion. Does anybody have an idea on how to do this?
See Question&Answers more detail:osThere 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);