OpenLayers:如何计算两点之间的距离?
如何使用墨卡托投影计算OpenLayers中两点之间的距离OpenLayers:如何计算两点之间的距离?,openlayers,distance,points,mercator,Openlayers,Distance,Points,Mercator,如何使用墨卡托投影计算OpenLayers中两点之间的距离 谢谢使用point1.距离(point2) 如果使用openlayers3 在两点之间实例化ol.geom.LineString对象并计算线的长度: this.distanceBetweenPoints = function(latlng1, latlng2){ var line = new ol.geom.LineString([latlng1, latlng2]); r
谢谢使用
point1.距离(point2)
如果使用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;
}
尝试
getLength
获取几何体的球形长度:
import LineString from 'ol/geom/LineString';
import {getLength} from 'ol/sphere';
const line = new LineString([coordStart, coordEnd]);
const distance = getLength(line);
API:请在您的答案中添加一些解释,以便其他人可以从中学习
import LineString from 'ol/geom/LineString';
import {getLength} from 'ol/sphere';
const line = new LineString([coordStart, coordEnd]);
const distance = getLength(line);