Monday, May 20, 2024
180
rated 0 times [  184] [ 4]  / answers: 1 / hits: 19713  / 10 Years ago, sat, september 27, 2014, 12:00:00

Using OpenLayers 3, how can I determine the distance between two points in the Spherical Mercator (SRID: 3857) projection?



I know that distanceTo was used in OpenLayers 2



point1.distanceTo(point2)


I looked through the OpenLayers 3 docs, but I'm not finding anything similar...


More From » openlayers-3

 Answers
436

You can use the Sphere object to calculate the distance between two coordinates like this:



var distance = ol.sphere.WGS84.haversineDistance([0,0],[180,0]); 
//20037508.34 meters


Sphere provides also various algorithms to calculate the distance like cosine,equirectangular etc.
You can also create the Sphere object with the radius of a different ellipsoid.



I don't know why the docs are not online, but you can check the methods available from the source code of the sphere object: https://github.com/openlayers/ol3/blob/master/src/ol/sphere.js



I personally think that looking at the source code is the best way to find answers about OpenLayers3 ;)


[#69312] Thursday, September 25, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
naya

Total Points: 60
Total Questions: 87
Total Answers: 87

Location: Guam
Member since Fri, Jun 18, 2021
3 Years ago
;