Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
525 views
in Technique[技术] by (71.8m points)

javascript - OpenLayers 3: How to calculate distance between 2 points?

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...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 ;)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...