【发布时间】:2013-04-06 19:04:47
【问题描述】:
我有一个谷歌地图,用户可以在其中搜索输入字段中的起点和终点,然后在绘制路线后,他们可以通过添加和拖动航点来自定义路线。提交时,路线会保存为 XML,然后在需要时在新地图上重新绘制。问题是,此刻我只有保存到 XML 的起点和终点。
我一直在使用此文档:https://developers.google.com/maps/documentation/javascript/directions#Waypoints,但我完全被难住了。
我建议的方式与起点和终点相同 - 将 lat 和 long 值保存到 XML 中。下面的 JS 获取航点的数据。我已经控制台记录了它以查看数组的结构。我在这里遇到的问题是我不知道如何遍历每个航路点(如果有多个航路点)并将每个航路点保存为一个新变量,以便我可以将它们添加到我的表单以进行 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();
【问题讨论】:
-
保存路线的代码是什么样的?向其中添加新的“通过”航点似乎非常简单。
-
我已经用更多细节更新了这个问题。谢谢。
-
我在想你会做类似this 的事情(基于example in the documentation)
-
但这不是将所有航点添加到一个输入值中吗?我将如何将所有这些数据添加到数据库中?
-
您要在数据库中保存什么?路线还是航路点?
标签: javascript xml google-maps