使用 OpenLayers 3,如何确定 Spherical Mercator (SRID: 3857) 投影中两点之间的距离?
我知道distanceTo
在 OpenLayers 2 中使用过
point1.distanceTo(point2)
我浏览了OpenLayers 3 文档,但没有找到类似的东西......
使用 OpenLayers 3,如何确定 Spherical Mercator (SRID: 3857) 投影中两点之间的距离?
我知道distanceTo
在 OpenLayers 2 中使用过
point1.distanceTo(point2)
我浏览了OpenLayers 3 文档,但没有找到类似的东西......
您可以使用 Sphere 对象来计算两个坐标之间的距离,如下所示:
var distance = ol.sphere.WGS84.haversineDistance([0,0],[180,0]);
//20037508.34 meters
Sphere 还提供了各种算法来计算距离,如余弦、等距矩形等。您还可以创建具有不同椭圆体半径的 Sphere 对象。
我不知道为什么文档不在线,但是您可以从球体对象的源代码中查看可用的方法:https://github.com/openlayers/ol3/blob/master/src/ol/sphere。 js
我个人认为查看源代码是找到有关 OpenLayers3 答案的最佳方式;)
我正在使用一个相当简单的解决方案。我在两点之间实例化一个 ol.geom.LineString 对象并计算线的长度:
this.distanceBetweenPoints = function(latlng1, latlng2){
var line = new ol.geom.LineString([latlng1, latlng2]);
return Math.round(line.getLength() * 100) / 100;
};
然后,您可以使用某种格式获得可读值:
this.formatDistance = function(length) {
if (length >= 1000) {
length = (Math.round(length / 1000 * 100) / 100) +
' ' + 'km';
} else {
length = Math.round(length) +
' ' + 'm';
}
return length;
}
编辑:新的计算方法
实际上,关于您使用的投影,距离可能是错误的。我们在 ol3 的 github 上对此进行了相当长的讨论,您可以在那里看到: https ://github.com/openlayers/ol3/issues/3533
总而言之,您需要使用该函数才能获得精确的计算:
/**
* format length output
* @param {ol.geom.LineString} line
* @return {string}
*/
export default function mapFormatLength(projection, line) {
var length;
var coordinates = line.getCoordinates();
length = 0;
for (var i = 0, ii = coordinates.length - 1; i < ii; ++i) {
var c1 = ol.proj.transform(coordinates[i], projection, 'EPSG:4326');
var c2 = ol.proj.transform(coordinates[i + 1], projection, 'EPSG:4326');
length += mapConst.wgs84Sphere.haversineDistance(c1, c2);
}
var output;
if (length > 1000) {
output = (Math.round(length / 1000 * 100) / 100) +
' ' + 'km';
} else {
output = (Math.round(length * 100) / 100) +
' ' + 'm';
}
return output;
}
只是为了增加一个选项。这不依赖于 ol3。
function toRad(x) {return x * Math.PI / 180;}
function SphericalCosinus(lat1, lon1, lat2, lon2) {
var R = 6371; // km
var dLon = toRad(lon2 - lon1),
lat1 = toRad(lat1),
lat2 = toRad(lat2),
d = Math.acos(Math.sin(lat1)*Math.sin(lat2) + Math.cos(lat1)*Math.cos(lat2) * Math.cos(dLon)) * R;
return d;
}
我为自己写了这个我希望它会很有用,它以米为单位返回距离:
function getCoordsDistance(firstPoint, secondPoint, projection) {
projection = projection || 'EPSG:4326';
length = 0;
var sourceProj = mapObj.getView().getProjection();
var c1 = ol.proj.transform(firstPoint, sourceProj, projection);
var c2 = ol.proj.transform(secondPoint, sourceProj, projection);
var wgs84Sphere = new ol.Sphere(6378137);
length += wgs84Sphere.haversineDistance(c1, c2);
return length;
}
function getCoordsDistance(latlng1, latlng2) {
var markers = [];
markers.push(ol.proj.transform(latlng1, 'EPSG:4326', map.getView().getProjection()));
markers.push(ol.proj.transform(latlng2, 'EPSG:4326', map.getView().getProjection()));
var line = new ol.geom.LineString(markers, 'XY');
var length = Math.round(line.getLength() * 100) / 100;
if (length >= 1000) {
length = (Math.round(length / 1000 * 100) / 100) +
' ' + 'km';
} else {
length = Math.round(length) +
' ' + 'm';
}
return length;
}