나는지도상의 여러 지점 사이의 거리를 계산하는 데 어려움을 겪고 있습니다.거리 수학 자바 스크립트 및 루프에서 구현?
저는 첫 번째 좌표 [ "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 주차지도 API 기능 YMaps.Geopoint를 사용 말아야이 좌표에서지도에 Geopoints로 만들려면 :
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를 사용하는 점 사이의 거리를 계산합니다. 거리 (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
(나는 또한 형식을 각 결과를 변환() 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]));
}
이 루프 작동하지만 잘못된 결과를 반환합니다. 아무도 루프의 문제를 조언 할 수 있습니까? 아마도이 루프에서 다른 루프를 다른 루프를 사용하여 다른 포인트를 필요한만큼 많이 반환해야 할 필요가 있다고 생각합니다. 감사합니다. .
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
덕분입니다. 그리고 동적 변수 window [ "temp _"+ i]를 루프 외부에서 사용하지 않고 어떻게 할 수 있습니까? – moogeek
wicked! 그것은 일했다 !!! 너는 유머 감각이있다! – moogeek