【问题标题】:MKCoordinateRegion distance between center and bordersMKCoordinateRegion 中心和边界之间的距离
【发布时间】:2012-01-06 17:13:13
【问题描述】:

(根据评论修改问题:)

好的,我会尝试以其他方式询问...如何获取此圆圈叠加层的边框坐标:


(原问题:)

我的 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/,它计算两个坐标之间的距离。当我作为 FROM 坐标输入时:51.509980 和经度:-0.133700(伦敦)和 TO 坐标:

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.40 英里,而不是预期的 10 英里。

截图:

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

感谢回复

【问题讨论】:

    标签: objective-c mkmapview core-location coordinate-systems mkcoordinateregion


    【解决方案1】:

    (根据评论修改答案:)

    如果您的意思是想要该圆形叠加层的边界矩形的坐标,请使用叠加层的 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 英里,那么肯定会调整至少一个跨度。即使您要求根据视图比例设置的跨度,如果地图视图无法以该缩放级别显示图块(或者如果您没有使用地球的曲率)。

    接下来,latitudeDeltalongitudeDelta 定义了区域的整个高度和宽度(不是到中心坐标的距离)。

    因此,您在屏幕截图中的测试无法与跨度增量进行比较。在屏幕截图中,您正在计算从中心坐标到最小纬度和经度(左下角)的距离,但跨度增量从右到左和从下到上一直延伸。 (正因为如此,你会认为从中心到角落的距离应该小于 delta——而不是更多。它更短,但由于上述原因,delta 也增加到了 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];
    

    【讨论】:

    【解决方案2】:

    在您的网络表单屏幕截图中,您得到的距离不同,因为您正在测量的线(中心到 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]; 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-21
      • 1970-01-01
      • 2014-04-02
      • 2018-05-03
      • 2021-12-28
      • 1970-01-01
      • 2019-05-17
      • 1970-01-01
      相关资源
      最近更新 更多