我在與地圖上的幾個計算點之間的距離的困難: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"]
];
接下來,從這個COORDS使地圖上的Geopoints我建議立即進行刪除使用特殊Yandex的地圖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函數「點1點之間的距離。距離(點2)「:
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
這工作得很好(我也相互轉換結果格式化(導致 公里)),其結果是:
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]));
}
這個循環的作品,但將返回錯誤的結果。任何人都可以建議循環中出現什麼問題嗎? 我想我可能需要在這個循環中使用另一個循環,它會根據需要返回其他點的彙總很多次。 謝謝。
它不使用parseFloat就可以解析,但我的難點在於實現這種依賴性: var d1 = a.distance(b); //距離1 var d2 = a.distance(b)+ b.distance(c); //距離2 var d3 = a.distance(b)+ b.distance(c)+ c.distance(d); ... 其中每次計算下一點加 – moogeek 2010-02-20 11:06:52
謝謝。我怎麼能做到這一點,而不使用循環外的動態變量窗口[「temp _」+ i]? – moogeek 2010-02-20 11:27:39
邪惡!有效!!!你真是太棒了! – moogeek 2010-02-20 11:33:32