Maps 为什么openlayers 4的距离计算方式不同
Openlayers 4“HaversinedDistance”计算两个点之间的距离不同(与ol.Sphere.getLength()相比) 为什么? 在这种情况下非常有用 getLength()和haversineDistance()最终将使用相同的算法计算距离:Maps 为什么openlayers 4的距离计算方式不同,maps,openlayers,Maps,Openlayers,Openlayers 4“HaversinedDistance”计算两个点之间的距离不同(与ol.Sphere.getLength()相比) 为什么? 在这种情况下非常有用 getLength()和haversineDistance()最终将使用相同的算法计算距离: ol.Sphere.getDistance_ = function(c1, c2, radius) { var lat1 = ol.math.toRadians(c1[1]); var lat2 = ol.math
ol.Sphere.getDistance_ = function(c1, c2, radius) {
var lat1 = ol.math.toRadians(c1[1]);
var lat2 = ol.math.toRadians(c2[1]);
var deltaLatBy2 = (lat2 - lat1) / 2;
var deltaLonBy2 = ol.math.toRadians(c2[0] - c1[0]) / 2;
var a = Math.sin(deltaLatBy2) * Math.sin(deltaLatBy2) + Math.sin(deltaLonBy2) * Math.sin(deltaLonBy2) * Math.cos(lat1) * Math.cos(lat2);
return 2 * radius * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
};
主要区别在于HaversinedDistance()方法仅适用于两个坐标,而getLength()功能更强大,它将对任意数量的坐标的距离求和,从而管理不同类型的几何体
因此,对于两点之间的简单直线距离,您不应该看到任何差异
如果有,可能是因为您使用的是不同的几何体类型,或者是因为您没有在同一投影中工作。getLength()方法默认用于EPSG:3857/EPSG:4326
问题出在投影上
var wgs84Sphere = new ol.Sphere(6371008.8);
var length = wgs84Sphere.haversineDistance(cor1, cor2); // Must be 'EPSG:4326'
Openlayer与“EPSG:3857”(Mercator)一起“工作”(大部分)。
但是“ol.Sphere.HaversinedDistance”需要'EPSG:4326'(Lat/Long)
文档中没有写这篇文章
默认情况下,ol.Sphere.getLength()几何体是“EPSG:3857”。以下专用函数转换为“EPSG:4326”
ol.Sphere.getLength = function(geometry, opt_options) {
var options = opt_options || {};
var radius = options.radius || ol.Sphere.DEFAULT_RADIUS;
var projection = options.projection || 'EPSG:3857';
geometry = geometry.clone().transform(projection, 'EPSG:4326');
var type = geometry.getType();
[...]
var wgs84Sphere = new ol.Sphere(6371008.8);
var length = wgs84Sphere.haversineDistance(cor1, cor2); // Must be 'EPSG:4326'