[問題]google api 多點距離

看板Ajax作者 (code)時間12年前 (2013/01/24 14:17), 編輯推噓0(003)
留言3則, 1人參與, 最新討論串1/2 (看更多)
小弟最近在用google api 計算兩點距離 可是想要把好幾個點距離全部加總起來,但因為對javascript很不熟 以下是網路找的範例,測試過是可以run,但是只有兩點距離. 想要用陣列把經緯度放入,然後多跑幾次迴圈,跑出每個點之間距離 然後加總 但是卻try不出來 請問有大大可以解惑嗎? var directionsService = new google.maps.DirectionsService(); var km=0; // 總公里數 var lat1 = new Array(2); var lat2 = new Array(2); lat1[0] = "24.989578"; //假設變數 lat1[1] ="121.313564"; //假設變數 var a= parseFloat("24.989578", 10); var b= parseFloat("121.313564", 10); // DirectionsRequest var request = { // 起點 origin: new google.maps.LatLng(a,b), // <----- 陣列放入的話 // 終點 destination: new google.maps.LatLng( 24.954118,121.225823), // <----- 陣列放入的話 waypoints: [], // 路線最佳化 optimizeWaypoints: true, // 交通模式,目前有 開車/步行 travelMode:google.maps.TravelMode.DRIVING }; directionsService.route(request, function(response, status) { if (status == google.maps.DirectionsStatus.OK) { var route = response.routes[0]; // 取得距離 // console.log(route.legs[0].distance.text); // 取得從起點至終點的大約時間 // console.log(route.legs[0].duration.text); document.getElementById('msg').innerHTML = route.legs[0].distance.text + ', ' + route.legs[0].duration.text ; /// route.legs[0].distance.text 加總 km+=route.legs[0].distance; } }); -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.138.152.14

01/24 16:19, , 1F
差不多解決,目前的問題是waypoints: [{location: "Taipei"}
01/24 16:19, 1F

01/24 16:20, , 2F
,{location: "Taichung"}] 如何把裡面的值改成 經緯度?
01/24 16:20, 2F

01/24 16:21, , 3F
以解決 {location: "24.306636,120.930176"}
01/24 16:21, 3F
文章代碼(AID): #1H0D7Wjo (Ajax)
文章代碼(AID): #1H0D7Wjo (Ajax)