OpenLayers5在EPSG:4326投影坐标系下测量长度和面积


ol/sphere里有getLength()和getArea()可以用来测量距离和区域面积,默认的投影坐标系是EPSG:3857, 其中有个options的参数,可以设置投影坐标系。使用时, 可以先把几何图形转换成‘EPSG:3857 ’的投影方式,然后再使用getLength和getArea。也可以设置options参数,下面是代码:

//地图使用的是EPSG:4326投影坐标系
/**
 * 格式化长度输出
 * @param {module:ol/geom/LineString~LineString} line The line.
 * return {string} The formatted length.
 */
var formatLength = function (line) {
	var sourceProj = map.getView().getProjection();//获取投影坐标系
	//方法1
	// var geom = (line.clone().transform(sourceProj, 'EPSG:3857'));
	// var length = getLength(geom);
	//方法2
	var length = getLength(line, {projection: sourceProj/*, radius: 6371008.8*/});

	var output;
	if (length > 100) {
		output = (Math.round(length / 1000 * 100) / 100) +
				' ' + '千米';
	} else {
		output = (Math.round(length * 100) / 100) +
				' ' + '米';
	}
	return output;
};

/**
 * 格式化面积输出
 * @param {module:ol/geom/Polygon~Polygon}polygon The Polygon.
 * @return {string} Formatted area;
 */
var formatArea = function(polygon){
	var sourceProj = map.getView().getProjection();//获取投影坐标系
	//方法1
	// var geom = (polygon.clone().transform(sourceProj, 'EPSG:3857'));
	// var area = getArea(geom);
	//方法2
	var area = getArea(polygon, {projection: sourceProj/*, radius: 6371008.8*/})
	
	var output;
	if(area > 10000){
		output = (Math.round(area / 1000000 * 100) / 100) + ' ' + 'km<sup>2<sup>';
	}else{
		output = (Math.round(area * 100) / 100) + ' ' + 'm<sup>2<sup>';
	}
	return output;
};

转载自:https://blog.csdn.net/guimaxingtian/article/details/85243396

You may also like...

退出移动版