JavaScript 中的距离数学并在循环中实现它?
我在计算地图上几个点之间的距离时遇到困难:
我有一个坐标数组,其中第一个坐标 ["30.327547", "59.919676"] 是行程的开始,其他坐标是进站:
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"]
];
接下来,要从此坐标在地图上创建地理点,我应该使用特殊的 Yandex Maps API 函数 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);
最后,要计算点之间的距离,我使用另一个 API 函数“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
这效果很好(我还将每个结果转换为格式 (result km) ),结果是:
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."}
我实际上想要在循环内进行此操作:
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]));
}
该循环有效,但返回错误的结果。有人可以建议循环中出了什么问题吗? 我想我可能需要在这个循环中使用另一个循环,该循环将根据需要(需要)多次返回其他点的总和。 谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
J 变量是您声明的二维字符串数组,从我可以看到 YMaps.GeoPoint 函数需要一个数字。因此,请尝试解析:
更新:
这是您的代码的问题:
在每次迭代中,您重新声明
formula2
变量以及计算当前点之间的人类距离。您需要累加总和,因此在循环外部声明变量并在内部添加它:请注意,循环在
J.length - 1
处结束:例如。如果有 7 个点,则有 6 个距离。The
J
variable is a two dimensional array of strings as you declared it and from what I can see theYMaps.GeoPoint
function expects a number. So try parsing:UPDATE:
Here's what's wrong with your code:
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:Notice that the loop ends at
J.length - 1
: ex. for 7 points you have 6 distances.