Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 289
sfletche Avatar asked Sep 27 '14 05:09

sfletche


People also ask

What is the formula for distance between two points?

What is Distance Between Two Points Formula? Distance between two points is the length of the line segment that connects the two points in a plane. The formula to find the distance between the two points is usually given by d=√((x2 – x1)² + (y2 – y1)²).

How do you find the distance between two points in r3?

To find the distance between two points, take the coordinates of two points such as (x1, y1) and (x2, y2) Use the distance formula (i.e) square root of (x2 – x1)2 + (y2 – y1) For this formula, calculate the horizontal and vertical distance between two points.


3 Answers

Just to add one more option. This is not ol3 dependent.

function toRad(x) {return x * Math.PI / 180;}

function SphericalCosinus(lat1, lon1, lat2, lon2) {

    var R = 6371; // km
    var dLon = toRad(lon2 - lon1),
        lat1 = toRad(lat1),
        lat2 = toRad(lat2),
        d = Math.acos(Math.sin(lat1)*Math.sin(lat2) + Math.cos(lat1)*Math.cos(lat2) * Math.cos(dLon)) * R;


    return d;
}
like image 84
Jonatas Walker Avatar answered Oct 14 '22 07:10

Jonatas Walker


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

like image 44
markov00 Avatar answered Oct 14 '22 06:10

markov00


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:

        this.distanceBetweenPoints = function(latlng1, latlng2){
            var line = new ol.geom.LineString([latlng1, latlng2]);
            return Math.round(line.getLength() * 100) / 100;
        };

You can then get a readable value using some formating:

        this.formatDistance = function(length) {
            if (length >= 1000) {
                length = (Math.round(length / 1000 * 100) / 100) +
                ' ' + 'km';
            } else {
                length = Math.round(length) +
                ' ' + 'm';
            }
            return length;
        }

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:

/**
 * format length output
 * @param {ol.geom.LineString} line
 * @return {string}
 */
export default function mapFormatLength(projection, line) {
  var length;
  var coordinates = line.getCoordinates();
  length = 0;
  for (var i = 0, ii = coordinates.length - 1; i < ii; ++i) {
    var c1 = ol.proj.transform(coordinates[i], projection, 'EPSG:4326');
    var c2 = ol.proj.transform(coordinates[i + 1], projection, 'EPSG:4326');
    length += mapConst.wgs84Sphere.haversineDistance(c1, c2);
  }
  var output;
  if (length > 1000) {
    output = (Math.round(length / 1000 * 100) / 100) +
    ' ' + 'km';
  } else {
    output = (Math.round(length * 100) / 100) +
    ' ' + 'm';
  }
  return output;
}
like image 5
Alexandre Mélard Avatar answered Oct 14 '22 05:10

Alexandre Mélard