本文介绍了如何使两个MKCoordinateRegion之间的联合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图做两个MKCoordinateRegion之间的联合。
解决方案
有一个 MKMapRectUnion
函数接受两个 MKMapRects
,所以你可以先将每个 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);
I'm trying to do the union between two MKCoordinateRegion. Does anybody have an idea on how to do this?
解决方案 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);
这篇关于如何使两个MKCoordinateRegion之间的联合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!