Openlayers 3 openlayers ol3 linestring getLength未返回预期值

Openlayers 3 openlayers ol3 linestring getLength未返回预期值,openlayers-3,Openlayers 3,我正在使用getLength检索linestring长度 同一部分: 1-当使用谷歌地图测量工具时,我得到228m 2-使用IGN geoportail测量工具时,我得到228m 3-当我使用e.feature.getGeometry().getLength()时,我得到330m 以下是平面坐标: e、 feature.getGeometry().getFlatCoordinates(): [571382.42140415935723486.068714521571593.81756051057

我正在使用getLength检索linestring长度

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

以下是平面坐标: e、 feature.getGeometry().getFlatCoordinates(): [571382.42140415935723486.068714521571593.8175605105723741.65502785]

在4326: [5.132815622245775、45.644023326845485、5.134714626228319、45.64562844964627]

当我检查ol3或谷歌地图上的坐标位置时,我得到了相同的点。差异一定来自于计算

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

geometry.getLength()
返回地图视图投影中的长度,通常为球形墨卡托。球面墨卡托距离以
1/cos(纬度)
的速率拉伸;在您的示例中:
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椭球体而不是球体上进行测量。

geometry.getLength()
返回地图视图投影中的长度,通常为球形墨卡托。球面墨卡托距离以
1/cos(纬度)
的速率拉伸;在您的示例中:
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椭球体而不是球体上进行测量。

geometry.getLength()
返回地图视图投影中的长度,通常为球形墨卡托。球面墨卡托距离以
1/cos(纬度)
的速率拉伸;在您的示例中:
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椭球体而不是球体上进行测量。

geometry.getLength()
返回地图视图投影中的长度,通常为球形墨卡托。球面墨卡托距离以
1/cos(纬度)
的速率拉伸;在您的示例中:
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椭球体而不是球体上进行测量。

上述答案是正确的,尽管如果您试图获得具有多个位置的线点的长度,它将仅在第一个位置之间进行计算

下面是一个关于使用多个位置的直线点的小补充:

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);
var geometry=feature.getGeometry();
//获取几何体的点(为简单起见,假设2点)
var坐标=geometry.getCoordinates();
//将点从地图投影(通常为球形墨卡托)转换为WGS84
var mapProjection=map.getView().getProjection();
//创建要在其上测量的球体
var wgs84sphere=新的ol.Sphere(6378137);//WGS84地球半径'
var-dist=0;
//循环遍历所有坐标
对于(变量i=0;i
上述答案是正确的,但如果您试图获得具有多个位置的线点的长度,它将仅在前几个位置之间进行计算

下面是一个关于使用多个位置的直线点的小补充:

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);
var geometry=feature.getGeometry();
//获取几何体的点(为简单起见,假设2点)
var坐标=geometry.getCoordinates();
//将点从地图投影(通常为球形墨卡托)转换为WGS84
var mapProjection=map.getView().getProjection();
//创建要在其上测量的球体
var wgs84sphere=新的ol.Sphere(6378137);//WGS84地球半径'
var-dist=0;
//循环遍历所有坐标
对于(变量i=0;i
上述答案是正确的,但如果您试图获得具有多个位置的线点的长度,它将仅在前几个位置之间进行计算

下面是一个关于使用多个位置的直线点的小补充:

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);
var geometry=feature.getGeometry();
//获取几何体的点(为简单起见,假设2点)
var坐标=geometry.getCoordinates();
//将点从地图投影(通常为球形墨卡托)转换为WGS84
var mapProjection=map.getView().getProjection();
//创建要在其上测量的球体
var wgs84sphere=新的ol.Sphere(6378137);//WGS84地球半径'
var-dist=0;
//循环遍历所有坐标
对于(变量i=0;i
上述答案是正确的,但如果您试图获得具有多个位置的线点的长度,它将仅在前几个位置之间进行计算

下面是一个关于使用多个位置的直线点的小补充:

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);
var geometry=feature.getGeometry();
//获取几何体的点(为简单起见,假设2点)
var坐标=geometry.getCoordinates();
//将点从地图投影(通常为球形墨卡托)转换为WGS84
var mapProjection=map.getView().getProjection();
//创建要在其上测量的球体
变量wgs84sphere=新的ol.Sphere