我有下面的代码来计算从 A(60.054166,-35.542222)到 B(-48.175833,-8.541111)的距离。以上坐标采用 EPSG:4326 系统(这是标准 GPS 纬度/经度坐标)。
默认测量单位是角度,所以我将所有内容转换为 EPSG:3857,其中测量单位是米。
static void Main(string[] args) {
/* we will convert everything from 4326 (lat/lon) to 3857 since the latter's default unit is meters instead of rads */
geomFactory = new GeometryFactory(new PrecisionModel(1e6), 3857);
var cs4326 = ProjNet.CoordinateSystems.GeographicCoordinateSystem.WGS84;
var cs3857 = ProjNet.CoordinateSystems.ProjectedCoordinateSystem.WebMercator;
var ctFactory = new ProjNet.CoordinateSystems.Transformations.CoordinateTransformationFactory();
var ct = ctFactory.CreateFromCoordinateSystems(cs4326, cs3857);
var mt = ct.MathTransform;
var p1 = new double[] { -35.5422222, 60.054166 };
var p2 = new double[] { -8.541111, -48.1758333 };
var r1 = mt.Transform(p1);
var r2 = mt.Transform(p2);
var lineString = geomFactory.CreateLineString(new Coordinate[] { new Coordinate(r1[0], r1[1]), new Coordinate(r2[0], r2[1]) });
Console.WriteLine("Distance is: " + lineString.Length / 1000 + " km.");
Console.ReadLine();
}
我得到的输出是: 14,855.22 公里,但谷歌地图测量工具输出 12,343.32 相同的坐标。
有些东西告诉我谷歌的输出是正确的,所以问题是我在这里遗漏了什么?