openlayers自定义工具条(底图为天地图)
openlayers官网发现测距工具条量测距离不准,官网的代码直接copy下来不可以使用,测距的代码需要修改,需要直接计算两点之间的距离
formatLength = function (line) {
let coordinates = line.getCoordinates();
length = 0;
let sourceProj = map.getView().getProjection();
for (let i = 0, ii = coordinates.length - 1; i < ii; ++i) {
let c1 = transform(coordinates[i], sourceProj, 'EPSG:4326');
let c2 = transform(coordinates[i + 1], sourceProj, 'EPSG:4326');
length += getDistance(c1, c2);
}
let outputIn;
if (length > 100) {
outputIn = (Math.round(length / 1000 * 100) / 100) + ' ' + 'km';
} else {
outputIn = (Math.round(length * 100) / 100) + ' ' + 'm';
}
return outputIn;
};
![在这里插入图片描述](https://img-blog.csdnimg.cn/ba3accaa0bce4621a4c303f69c72c697.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA6Zi_6ZmI55qE54i454i4,size_20,color_FFFFFF,t_70,g_se,x_16)