javascript OpenLayers 3:如何计算两点之间的距离?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26071490/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
OpenLayers 3: How to calculate distance between 2 points?
提问by sfletche
Using OpenLayers 3, how can I determine the distance between two points in the Spherical Mercator (SRID: 3857) projection?
使用 OpenLayers 3,如何确定 Spherical Mercator (SRID: 3857) 投影中两点之间的距离?
I know that distanceTo
was used in OpenLayers 2
我知道distanceTo
在 OpenLayers 2 中使用过
point1.distanceTo(point2)
I looked through the OpenLayers 3 docs, but I'm not finding anything similar...
我浏览了OpenLayers 3 文档,但没有找到类似的东西......
回答by markov00
You can use the Sphere object to calculate the distance between two coordinates like this:
您可以使用 Sphere 对象来计算两个坐标之间的距离,如下所示:
var distance = ol.sphere.WGS84.haversineDistance([0,0],[180,0]);
//20037508.34 meters
Sphere provides also various algorithms to calculate the distance like cosine,equirectangular etc. You can also create the Sphere object with the radius of a different ellipsoid.
Sphere 还提供了各种算法来计算距离,如余弦、等距矩形等。您还可以创建具有不同椭圆体半径的 Sphere 对象。
I don't know why the docs are not online, but you can check the methods available from the source code of the sphere object: https://github.com/openlayers/ol3/blob/master/src/ol/sphere.js
我不知道为什么文档不在线,但是您可以从 sphere 对象的源代码中查看可用的方法:https: //github.com/openlayers/ol3/blob/master/src/ol/sphere。 js
I personally think that looking at the source code is the best way to find answers about OpenLayers3 ;)
我个人认为查看源代码是找到有关 OpenLayers3 答案的最佳方式;)
回答by Alexandre Mélard
I'm using a fairly simple solution. I instanciate a ol.geom.LineString object between the two points and calculate the length of the line:
我正在使用一个相当简单的解决方案。我在两点之间实例化一个 ol.geom.LineString 对象并计算线的长度:
this.distanceBetweenPoints = function(latlng1, latlng2){
var line = new ol.geom.LineString([latlng1, latlng2]);
return Math.round(line.getLength() * 100) / 100;
};
You can then get a readable value using some formating:
然后,您可以使用某种格式获取可读值:
this.formatDistance = function(length) {
if (length >= 1000) {
length = (Math.round(length / 1000 * 100) / 100) +
' ' + 'km';
} else {
length = Math.round(length) +
' ' + 'm';
}
return length;
}
EDIT: New method of calcul
编辑:新的计算方法
Actualy, the distance can be false regarding the projection that you use. We had a fairly long discussion about this on ol3's github, you can see it there: https://github.com/openlayers/ol3/issues/3533
实际上,对于您使用的投影,距离可能是错误的。我们在 ol3 的 github 上对此进行了相当长的讨论,您可以在那里看到:https: //github.com/openlayers/ol3/issues/3533
To summarize, you need to use that function in order to get exact calcul:
总而言之,您需要使用该函数来获得精确的计算:
/**
* 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;
}
回答by Jonatas Walker
Just to add one more option. This is not ol3 dependent.
只是为了增加一个选项。这不依赖于 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;
}
回答by MSS
I wrote this one for myself I hop it'll be useful, It return distance in metters:
我为自己写了这个,我希望它会很有用,它以米为单位返回距离:
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;
}
回答by user2573099
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;
}