简体   繁体   English

如何使用Google Maps Waypoint重绘路线

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

I've got a Google Map where users search for a start and end point in input fields, then once the route has been drawn they can customise the route by adding and dragging waypoints. 我有一个Google Map,用户可以在输入字段中搜索起点和终点,然后在绘制路线后就可以通过添加和拖动航路点来自定义路线。 On submit the route is saved as XML, and then redrawn on a new map when needed. 提交后,该路线将另存为XML,然后在需要时在新地图上重绘。 The problem is that at the minute I only have the start and end points saving to the XML. 问题是,目前我只有起点和终点保存到XML。

I've been working from this documentation: https://developers.google.com/maps/documentation/javascript/directions#Waypoints , but I'm completely stumped here. 我一直在使用以下文档: https : //developers.google.com/maps/documentation/javascript/directions#Waypoints ,但我在这里完全迷住了。

The way I have proposed to do it is the same as the start and end points - saving the lat and long values into the XML. 我提出的方法与起点和终点相同-将经度和纬度值保存到XML中。 The JS below gets the data of the waypoints. 下面的JS获取航点的数据。 I've console.logged it to see how the arrays are structured. 我已经在console.logged它,以查看数组的结构。 The problem I have here is that I don't know how to loop through each waypoint (if there is more than one) and save each one as a new variable so that I can add them to my form for a PHP submission. 我这里的问题是,我不知道如何遍历每个路标(如果有多个),并将每个路标另存为新变量,以便将其添加到表单中以进行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);
});

Like I said though, there is probably an easier way, above is just what I've tried so far. 就像我说过的那样,可能有一种更简单的方法,上面就是我到目前为止已经尝试过的方法。 This is the result of the console.log(waypoints)... 这是console.log(waypoints)...的结果

在此处输入图片说明

The JS which gives hidden inputs their values is below... 给出隐藏输入值的JS如下...

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

This is the PHP which saves it as XML... 这是将其另存为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();

When your problem is only the loop through the waypoints, it goes like this: 当您的问题只是通过航路点的循环时,它将如下所示:

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