Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 625385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:18:13+00:00 2026-05-13T19:18:13+00:00

I’m having the difficulties with calculating distance between several points on the map: I

  • 0

I’m having the difficulties with calculating distance between several points on the map:

I have an array of coordinats where the first coord [“30.327547”, “59.919676”] is the begining of the trip and other are pit stops:

var J = [
    ["30.327547", "59.919676"],
    ["29.84964", "58.737619"],
    ["28.250252", "57.785994"],
    ["30.098912", "55.175885"],
    ["30.37357", "54.503783"],
    ["27.572056", "53.898325"],
    ["26.000193", "53.11856"]

];

Next, to make the Geopoints on the map from this coords I shoul use special Yandex Maps API function YMaps.Geopoint:

var a=new YMaps.GeoPoint(30.327547,59.919676);
var b=new YMaps.GeoPoint(29.84964,58.737619);
var c=new YMaps.GeoPoint(28.250252,57.785994);
var d=new YMaps.GeoPoint(30.098912,55.175885);
var e=new YMaps.GeoPoint(30.37357,54.503783);
var f=new YMaps.GeoPoint(27.572056,53.898325);
var g=new YMaps.GeoPoint(26.000193,53.11856);

Finally, to calculate distances between points i use another API function “point1.distance(point2)”:

var d1=a.distance(b);        //distance1
var d2=a.distance(b)+b.distance(c);    //distance2
var d3=a.distance(b)+b.distance(c)+c.distance(d);  //distance3
var d4=a.distance(b)+b.distance(c)+c.distance(d)+d.distance(e);   //distance4
var d5=a.distance(b)+b.distance(c)+c.distance(d)+d.distance(e)+e.distance(f);   //distance5
var d6=a.distance(b)+b.distance(c)+c.distance(d)+d.distance(e)+e.distance(f)+f.distance(g);   //distance6

This works pretty well (I also convert each result to format (result km) ) and the result is:

    console.log(YMaps.humanDistance(d1));console.log(YMaps.humanDistance(d2));console.log(YMaps.humanDistance(d3));
    console.log(YMaps.humanDistance(d4));console.log(YMaps.humanDistance(d5));console.log(YMaps.humanDistance(d6));

//{"Point1":"134 km.","Point2":"275 km.","Point3":"586 km.","Point4":"663 km.","Point5":"857 km.","Point6":"992 km."}

What I actually want to make this operations inside a loop:

for(var i=0;i<J.length;i++){

    // Iteratting through the array of points of J and creating Geoobjects on the map, dynamically putting them into public variables "temp_*"
    window["temp_" + i]=new YMaps.GeoPoint(J[i][0],J[i][1]);

    //next point
    var next=i+1;
    //master point (the begin of trip)
    var master=window["temp_0"];


    //calculating the distance between the master point and actual [i] point in the loop
    var formula=master.distance(window["temp_"+i]);

    // calculating the distance between the actual [i] point and the next in the loop
    var formula2=window["temp_" + i].distance(window["temp_"+next]);


    //summing and converting into human format and dinamically putting them into variables "result_*"
    window["result_"+i]=YMaps.humanDistance(formula+formula2);

    //logging the results
    console.log(YMaps.humanDistance(window["result_"+i]));
    }

This loop works but returns wrong results. Could anybody advice what is wrong in the loop?
I guess that probably i need to use another loop in this loop that woul return the summ of other points so many times as neccessary (requires).
Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T19:18:13+00:00Added an answer on May 13, 2026 at 7:18 pm

    The J variable is a two dimensional array of strings as you declared it and from what I can see the YMaps.GeoPoint function expects a number. So try parsing:

    window['temp_' + i] = new YMaps.GeoPoint(
        parseFloat(J[i][0]), 
        parseFloat(J[i][1])
    );
    

    UPDATE:

    Here’s what’s wrong with your code:

    var formula=master.distance(window["temp_"+i]);
    var formula2=window["temp_" + i].distance(window["temp_"+next]);
    window["result_"+i]=YMaps.humanDistance(formula+formula2);
    

    On each iteration you redeclare the formula2 variable and when you calculate the human distance between the current points. You need to accumulate the sum, so declare the variable outside the loop and add to it inside:

    var sum = 0;
    for(var i = 0; i < J.length - 1; i++) {
        var p1 = new YMaps.GeoPoint(J[i][0], J[i][1]);
        var p2 = new YMaps.GeoPoint(J[i + 1][0], J[i + 1][1]);
        sum += p1.distance(p2);
        console.log(YMaps.humanDistance(sum);
    }
    

    Notice that the loop ends at J.length - 1: ex. for 7 points you have 6 distances.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.