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