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...
I'm using a fairly simple solution. I instanciate a ol.geom.LineString object between the two points and calculate the length of the line:
You can then get a readable value using some formating:
EDIT: New method of calcul
Actualy, the distance can be false regarding the projection that you use. We had a fairly long discussion about this on ol3's github, you can see it there: https://github.com/openlayers/ol3/issues/3533
To summarize, you need to use that function in order to get exact calcul:
I wrote this one for myself I hop it'll be useful, It return distance in metters:
Just to add one more option. This is not ol3 dependent.
You can use the Sphere object to calculate the distance between two coordinates like this:
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 ;)