数学背后MKMetersPerMapPointAtLatitude

Sha*_*aun 14 math objective-c map-projections apple-maps

我正在尝试将一些Apple映射代码转换为Java.我有大部分转换正确,除了几个调用MKMetersPerMapPointAtLatitude

我有一个非常接近的解决方案......但这不是确切的,我不确定为什么不.有任何想法吗?

#import <Foundation/Foundation.h>
#import <Math.h>
@import MapKit;

#define MERCATOR_OFFSET 268435456.0 / 2.0
#define MERCATOR_RADIUS (MERCATOR_OFFSET/M_PI)
#define WGS84_RADIUS 6378137.0
#define POINTS_PER_METER (MERCATOR_RADIUS / WGS84_RADIUS)

double MyMetersPerMapPointAtLatitude(double latitude) {
    return 1.0 / (POINTS_PER_METER / cos(latitude * M_PI / 180.0));
}

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        double latitude = 33.861315;
        for (int i = 0; i < 100; i++) {
            double a = MKMetersPerMapPointAtLatitude(latitude);
            double b = MyMetersPerMapPointAtLatitude(latitude);

            NSLog(@"%f %f", a, b);
            latitude += .1;
        }
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

打印出以下内容

2015-05-19 09:13:00.334 Test[92619:5369062] 0.123522 0.123969
2015-05-19 09:13:00.335 Test[92619:5369062] 0.123379 0.123824
2015-05-19 09:13:00.335 Test[92619:5369062] 0.123236 0.123678
2015-05-19 09:13:00.335 Test[92619:5369062] 0.123092 0.123532
2015-05-19 09:13:00.335 Test[92619:5369062] 0.122948 0.123386
2015-05-19 09:13:00.335 Test[92619:5369062] 0.122804 0.123239
2015-05-19 09:13:00.335 Test[92619:5369062] 0.122659 0.123092
...etc
Run Code Online (Sandbox Code Playgroud)

Jay*_*nek 4

对于初学者,我们可以重新排列您的函数以使其更具可读性:

double MyMetersPerMapPointAtLatitude(double latitude) {
    return cos(latitude * M_PI / 180.0) / POINTS_PER_METER;
}
Run Code Online (Sandbox Code Playgroud)

现在,正如汤米指出的那样,问题是你没有考虑到地球变平的情况。您可以通过以下方式做到这一点:

double f = 1/298.257223563; // WGS84 flattening
double MyMetersPerMapPointAtLatitude(double latitude) {
    return (1-f) * cos(latitude * M_PI / 180.0) / POINTS_PER_METER;
}
Run Code Online (Sandbox Code Playgroud)

这使得错误下降到我将其归因于舍入和截断的程度。