openlayers ol3 linestring getLength not returning expected value -
i using getlength retrieve linestring length.
for same segment: 1- when using google map measure tool, 228m 2- when using ign geoportail measure tool, 228m 3- when use e.feature.getgeometry().getlength() 330m
here flat coordinates: e.feature.getgeometry().getflatcoordinates() : [571382.4214041593, 5723486.068714521, 571593.8175605105, 5723741.65502785]
in 4326: [5.132815622245775, 45.644023326845485, 5.134714626228319, 45.64562844964627]
when check coordinates position on either ol3 or google map, same points. difference must come calcul...
did miss , should not use getlength method? please give me direction if think not issue.
geometry.getlength()
returns length in map view projection, spherical mercator. spherical mercator distances stretched @ rate of 1/cos(latitude)
; in example: 228/330 ~ cos(45.64)
.
to real spherical distance:
var geometry = feature.getgeometry(); alert (geometry.getlength()); // points of geometry (for simplicity assume 2 points) var coordinates = geometry.getcoordinates(); // transform points map projection (usually spherical mercator) 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 measure on var wgs84sphere = new ol.sphere(6378137); // 1 of wgs84 earth radius' // distance on sphere var dist = wgs84sphere.haversinedistance(t1, t2); alert (dist);
for higher accuracy have measure on wgs84 ellipsoid instead of sphere.