簡體   English   中英

如何使用Google Maps Waypoint重繪路線

[英]How to use Google Maps Waypoints to redraw a Route

我有一個Google Map,用戶可以在輸入字段中搜索起點和終點,然后在繪制路線后就可以通過添加和拖動航路點來自定義路線。 提交后,該路線將另存為XML,然后在需要時在新地圖上重繪。 問題是,目前我只有起點和終點保存到XML。

我一直在使用以下文檔: https : //developers.google.com/maps/documentation/javascript/directions#Waypoints ,但我在這里完全迷住了。

我提出的方法與起點和終點相同-將經度和緯度值保存到XML中。 下面的JS獲取航點的數據。 我已經在console.logged它,以查看數組的結構。 我這里的問題是,我不知道如何遍歷每個路標(如果有多個),並將每個路標另存為新變量,以便將其添加到表單中以進行PHP提交。

//What to do when a waypoint has been added/changed
google.maps.event.addListener(directionsDisplay, 'directions_changed', function() {
    var waypoints = directionsDisplay.getDirections().routes[0].legs[0].via_waypoints;
    console.log(waypoints);
    console.log(waypoints[0].kb);
});

就像我說過的那樣,可能有一種更簡單的方法,上面就是我到目前為止已經嘗試過的方法。 這是console.log(waypoints)...的結果

在此處輸入圖片說明

給出隱藏輸入值的JS如下...

$('form.add-journix').submit(function() {
      $('#startlat').attr('value', startLatVal);
      $('#startlng').attr('value', startLongVal);
      $('#endlat').attr('value', endLatVal);
      $('#endlng').attr('value', endLongVal);
  });

這是將其另存為XML的PHP​​ ...

header("Content-type: text/xml"); 

    // Iterate through the rows, adding XML nodes for each

    while ($row = mysql_fetch_assoc($result)){  
      // ADD TO XML DOCUMENT NODE  
      $node = $dom->createElement("marker");  
      $newnode = $parnode->appendChild($node);   
      $newnode->setAttribute("title", $row['title']);  
      $newnode->setAttribute("description", $row['description']);  
      $newnode->setAttribute("startlat", $row['startlat']);  
      $newnode->setAttribute("startlng", $row['startlng']);  
      $newnode->setAttribute("endlat", $row['endlat']);  
      $newnode->setAttribute("endlng", $row['endlng']);  
    } 

    echo $dom->saveXML();

當您的問題只是通過航路點的循環時,它將如下所示:

for(var i=0;i<waypoints.length;++i){
  console.log(waypoints[i].lat(),waypoints[i].lng());
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM