Select to view content in your preferred language

How to calculate distance of a point along a polyline (from the origine) ?

727
0
09-18-2018 11:53 AM
SylvainKerdreux2
Emerging Contributor

Hello,

I did not find an easy way to calculate the distance (from the origine) of a point on a polyline using web app builder (API Javascript 3.25)

I have a polyline with 10 points and a point which is over the polyline. 

I do not want to use a SOE or any server solution to find the distance from the start of the polyline to this point.

I found only one way to do it : (pseudo code) :

let distance = 0;

let numberOfPoints = myPolyline.paths[0].length;

for (let i = 0 ; i < numberOfPoints - 1 ; i += 1){

   let myPath = [myPolyline.paths[0]myPolyline.paths[0][i+1]]

   if (this.isMyPointOnMyPath(myPath, myPoint)){

      distance += this.distanceBetweenTwoPoints(myPolyline.paths[0], myPoint);

      return distance;

   }

   else {

      distance += myPath.length;

   }

   return 0.0;

I think this solution a little bit complex (especially the function isMyPointOnMyPath where I need to use cross product vector).

Is there an easier way to get the distance using 3.xx javascript API ? 

I want to find the distance because I need to get a M value of my point on the polyline (I think it's not possible using web app Builder and 3.xx javascript api).

With my polyline I have two attributes (start M, End M) and it should be easy to get M value of my point.

best regards,

Sylvain.

0 Kudos
0 Replies