openlayers ol3 linestring getLength没有返回预期值

时间:2015-04-09 20:28:55

标签: openlayers-3

我使用getLength来检索线串长度。

对于同一部分: 1-当使用谷歌地图测量工具时,我得到228米 2-当使用IGN geoportail测量工具时,我得到228m 3-当我使用e.feature.getGeometry()。getLength()时我得到330米

以下是平面坐标: e.feature.getGeometry()。getFlatCoordinates(): [571382.4214041593,5723486.068714521,571593.8175605105,5723741.65502785]

4326年

: [5.132815622245775,45.644023326845485,5.134714626228319,45.64562844964627]

当我检查ol3或谷歌地图上的坐标位置时,我得到相同的分数。差异必须来自计算...

我错过了什么,我不应该使用getLength方法吗?如果您认为这不是问题,请给我一些指导。

2 个答案:

答案 0 :(得分:3)

geometry.getLength()返回地图视图投影中的长度,通常是球形墨卡托。球形墨卡托距离以1/cos(latitude)的速率拉伸;在您的示例中:228/330 ~ cos(45.64)

获得真正的球形距离:

var geometry = feature.getGeometry();

alert (geometry.getLength());

// get points of geometry (for simplicity assume 2 points)
var coordinates = geometry.getCoordinates();

// transform points from map projection (usually spherical mercator) to WGS84
var mapProjection = map.getView().getProjection();
var t1 = ol.proj.transform(coordinates[0], mapProjection, 'EPSG:4326');
var t2 = ol.proj.transform(coordinates[1], mapProjection, 'EPSG:4326');

// create sphere to measure on
var wgs84sphere = new ol.Sphere(6378137); // one of WGS84 earth radius'

// get distance on sphere
var dist = wgs84sphere.haversineDistance(t1, t2);

alert (dist);

为了获得更高的精度,您必须在WGS84椭圆体而不是球体上进行测量。

答案 1 :(得分:0)

上面的答案是正确的,尽管如果你想要获得一个有几个位置的LinePoint的长度,它只会在第一个之间进行计算。

以下是使用LinePoints处理多个位置的一小部分内容:

var geometry = feature.getGeometry();


// get points of geometry (for simplicity assume 2 points)
var coordinates = geometry.getCoordinates();

// transform points from map projection (usually spherical mercator) to WGS84
var mapProjection = map.getView().getProjection();

// create sphere to measure on
var wgs84sphere = new ol.Sphere(6378137); // one of WGS84 earth radius'

var dist = 0;
//loop through all coordinates
for(var i = 0; i < coordinates.length -1; i++) {
    var t1 = ol.proj.transform(coordinates[i], mapProjection, 'EPSG:4326');
    var t2 = ol.proj.transform(coordinates[i+1], mapProjection, 'EPSG:4326');
    // get distance on sphere
    dist += wgs84sphere.haversineDistance(t1, t2);
}
alert(dist);