下面是使用 C 语言实现 WGS84 坐标系和 UTM 坐标系之间转换的完整代码:
``` #include <stdio.h> #include <math.h>
#define pi 3.14159265358979323846 #define a 6378137.0 #define e 0.08181919084262188
// WGS84 坐标转 UTM 坐标 void wgs2utm(double lat, double lon, double *x, double *y, int *zone) { double latRad = lat * pi / 180.0; double lonRad = lon * pi / 180.0; double lonOrigin = floor((lon + 180.0) / 6.0) * 6.0 - 183.0; double lonOriginRad = lonOrigin * pi / 180.0; *zone = (int)((lon + 180.0) / 6.0) + 1;
double N = a / sqrt(1.0 - e * e * sin(latRad) * sin(latRad));double T = tan(latRad) * tan(latRad);double C = e * e * cos(latRad) * cos(latRad) / (1.0 - e * e);double A = (lonRad - lonOriginRad) * cos(latRad);double M = a * ((1.0 - e * e / 4.0 - 3.0 * e * e * e * e / 64.0 - 5.0 * e * e * e * e * e * e / 256.0) * latRad- (3.0 * e * e / 8.0 + 3.0 * e * e * e * e / 32.0 + 45.0 * e * e * e * e * e * e / 1024.0) * sin(2.0 * latRad)+ (15.0 * e * e * e * e / 256.0 + 45.0 * e * e * e * e * e * e / 1024.0) * sin(4.0 * latRad)- (35.0 * e * e * e * e * e * e / 3072.0) * sin(6.0 * latRad));*x = (double)(N * A * (1.0 + A * A * ((1.0 - T + C) / 6.0 + A * A * (5.0 - 18.0 * T + T * T + 72.0 * C - 58.0 * e * e) / 120.0)) + 500000.0);*y = (double)(M + N * tan(latRad) * (A * A * (1.0 / 2.0 + A * A * ((5.0 - T + 9.0 * C + 4.0 * C * C) / 24.0 + A * A * (61.0 - 58.0 * T + T * T + 600.0 * C - 330.0 * e * e) / 720.0))));
}
// UTM 坐标