【问题标题】:Why does MKMetersBetweenMapPoints give me different results when I swap the parameters?为什么 MKMetersBetweenMapPoints 在我交换参数时会给出不同的结果?
【发布时间】:2012-02-07 21:58:05
【问题描述】:

我实际上是在尝试计算MKMapPoints 的 x 和 y 坐标中最大和最小点之间的距离。

为此,我正在这样做(y 轴上的最大距离):

MKMapPoint test1, test2;
double dist;
test1.x = 0.0;
test1.y = 0.0;
test2.x = 0.0;
test2.y = MKMapSizeWorld.height;
dist = MKMetersBetweenMapPoints(test2, test1);
NSLog(@"Distance %f",dist);

我在控制台中得到 18997878.291251。但是当我将距离计算更改为:

dist = MKMetersBetweenMapPoints(test1, test2);

我得到 18873651.664238,所以我不明白有什么区别。我什至不知道我是否在做正确的事情来获得 x 和 y 轴上距离的最大值。

任何帮助将不胜感激。

【问题讨论】:

  • 日志行应该是 NSLog(@"Distance %f",dist);
  • 抱歉,打错了。变量名是 dist。 (更正)
  • 我相信使用MKMetersBetweenMapPoints 的正确方法是使用MKMapPointForCoordinateCLLocationCoordinate2D 结构转换为MKMapPoint 结构,然后使用MKMetersBetweenMapPoints 获得以米为单位的距离。这可能会避免任何内部范围错误。
  • 如果将 test2.y 设置为 MKMapSizeWorld.height - 1 会发生什么?

标签: iphone ios google-maps geolocation mapkit


【解决方案1】:

我猜这是一个算法问题。某种近似在达到一定精度时停止。这就是为什么那里没有换向。您可以尝试示例代码来获取地图上两点之间的距离,而无需使用 MKMapPoints:

- (float) distanceToLPU {

        useDelegate

        CLLocationCoordinate2D pointACoordinate = appDelegate.usrLoc;
        CLLocation *pointALocation = [[CLLocation alloc] initWithLatitude:pointACoordinate.latitude longitude:pointACoordinate.longitude];

        CLLocationCoordinate2D pointBCoordinate;
        pointBCoordinate.latitude = [self.latitude doubleValue];
        pointBCoordinate.longitude = [self.longitude doubleValue];

        CLLocation *pointBLocation = [[CLLocation alloc] initWithLatitude:pointBCoordinate.latitude longitude:pointBCoordinate.longitude];  

        float distanceMeters = [pointALocation distanceFromLocation:pointBLocation];  

        [pointALocation release];
        [pointBLocation release];  

        NSString *dist = [NSString stringWithFormat:@" (%.0f м)", distanceMeters];
        NSLog(@"Distance to this LPU:%@", dist);

        return distanceMeters;
    }

【讨论】:

    猜你喜欢
    • 2012-06-09
    • 2017-05-07
    • 1970-01-01
    • 2014-02-01
    • 2021-08-26
    • 1970-01-01
    • 2011-02-20
    • 2015-11-16
    • 2014-08-14
    相关资源
    最近更新 更多