MKCoordinateRegion中心点与边界之间的距离

4

(根据评论修订的问题:)

好吧,我会试着用另一种方式来问...我如何获得这个圆形覆盖层的边框坐标:

enter image description here


(原始问题:)

我的 iPhone 应用程序出现了奇怪的问题。我有一个 MKCoordinateRegion,其中心坐标纬度为 51.509980,经度为 -0.133700。我使用了方法 MKCoordinateRegionMakeWithDistance,并将距离设置为 16,093.44 米(10 英里)。

我想要获取该区域的边界点,因此我编写了以下代码:

MKCoordinateRegion region2 = self.myMapView.region;

float minLat = region2.center.latitude - (region2.span.latitudeDelta / 2.0);
float maxLat = region2.center.latitude + (region2.span.latitudeDelta / 2.0);

float minLong = region2.center.longitude - (region2.span.longitudeDelta / 2.0);
float maxLong = region2.center.longitude + (region2.span.longitudeDelta / 2.0);

我找到了一个用于测试的网站http://boulter.com/gps/distance/,它可以计算两个坐标之间的距离。当我输入起始坐标为:51.509980,经度为:-0.133700(伦敦),终止坐标为:

2011-11-26 01:15:42.830 NearMeTest[3911:11603] MinLAT 51.334381 and MaxLAT 51.684814
2011-11-26 01:15:42.830 NearMeTest[3911:11603] MinLONG -0.352936 and MaxLONG 0.086517

我明白那两个坐标之间的距离为15.4英里,而不是预期的10英里。

截图:

enter image description here

为什么会有这样的差异?当我尝试使用不同的中心坐标(东京、纽约)时,结果是正确的10英里。

感谢回复。

2个回答

3
如果您想要获取圆形图层覆盖物的边界矩形的坐标,请使用覆盖物的boundingMapRect属性:
//"theCircle" is the MKCircle overlay object

CLLocationCoordinate2D topLeftCoord = 
    MKCoordinateForMapPoint(theCircle.boundingMapRect.origin);

MKMapPoint bottomRightMapPoint = MKMapPointMake (
    MKMapRectGetMaxX(theCircle.boundingMapRect), 
    MKMapRectGetMaxY(theCircle.boundingMapRect));

CLLocationCoordinate2D bottomRightCoord = 
    MKCoordinateForMapPoint(bottomRightMapPoint);

首先,在地图视图上调用`setRegion`时,地图视图几乎总会修改您请求的区域,以使其适合于地图视图。此调整基于地图视图的形状以及它是否能在其固定缩放级别之一正确显示所请求的跨度。
例如,如果您的地图视图不是正方形并且要求在两个方向上有10英里的跨度,至少一个跨度肯定会被调整。即使您根据视图的比例设置了跨度,如果地图视图无法在该缩放级别下显示瓦片(或者您没有考虑到地球的曲率),它也可能会被调整。
接下来,`latitudeDelta`和`longitudeDelta`定义了整个区域的高度和宽度(而不是中心坐标的距离)。
因此,您在截图中的测试无法与跨度差进行比较。在截图中,您计算的是从中心坐标到最小纬度和经度(左下角)的距离,但跨度差跨越了从右到左和从下到上的整个范围。(因此,您会认为从中心到一个角落的距离应该比跨度小,而不是更大。它确实更短,但由于上述原因,跨度也增加到了超过10。)
最后,要获取角落坐标(左下角和右上角),这可能是一个更准确的方法:
CLLocationCoordinate2D bottomLeftCoord = 
    [myMapView convertPoint:CGPointMake(0, myMapView.frame.size.height) 
               toCoordinateFromView:myMapView];

CLLocationCoordinate2D topRightCoord = 
    [myMapView convertPoint:CGPointMake(myMapView.frame.size.width, 0) 
               toCoordinateFromView:myMapView];

好的,我会尝试用其他方式来问...我如何获取这个圆形覆盖层的边框坐标:http://img39.imageshack.us/img39/6346/screenshot20111127at113.png? - pprochazka72

0
在您的网络表单截图中,您得到了不同的距离,因为您测量的线(从中心到MinLAT / MinLONG)是对角线,并且延伸超出了圆形半径。
如果您按照@AnnaKarenina的答案操作,然后可以通过将每个CLLocationCoordinate2D转换为CLLocation来获得以米为单位的距离。
以下是如何获取英里半径:
CLLocation * centerLeftLocation = [[CLLocation alloc] 
                                   initWithLatitude:centerCoord.latitude 
                                   longitude:topLeftCoord.longitude];
CLLocation * centerLocation = [[CLLocation alloc] 
                               initWithLatitude:centerCoord.latitude 
                               longitude:centerCoord.longitude];

CLLocationDistance distanceInMeters = [centerLeftLocation distanceFromLocation:centerLocation]; 

float distanceInMiles = distanceInMeters / 1609.344f; 

[centerLeftLocation release]; 
[centerLocation release]; 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接