因此,开始和结束目的地正在通过并正在创建路线,但路线点未被拾取。
航点的Html
<div id="dynamicInput" class="form-group">
<label>Additional Destinations</label>
<input type="text" name="waypoints[]" class="form-control" autocomplete="on">
</div>
<input type="button" class="btn btn-danger" value="Add another destination" onClick="addInput('dynamicInput');">
Js使添加更多航路点功能工作
var counter = 1;
var limit = 10;
var i = 0;
function addInput(divName) {
if (counter == limit) {
alert("You have reached the limit of adding " + counter + " additional destinations");
} else {
var newbr = document.createElement('br');
var newinput = document.createElement("input");
newinput.setAttribute("name","waypoints[]");
newinput.setAttribute("autocompute","on");
newinput.setAttribute("type", "text");
newinput.setAttribute("class", "form-control");
// newin = (counter + 1) + "<input type=text name=waypoints[] autocomplete=on>";
document.getElementById(divName).appendChild(newbr);
document.getElementById(divName).appendChild(newinput);
counter++;
i++;
console.log("cntr=" + counter + " i=" + i + " waypoints[].length=" +document.getElementsByName("waypoints[]"));
// var inputw = waypoints[i];
var autocompletew = new google.maps.places.Autocomplete(newinput);
autocompletew.setComponentRestrictions({'country':['uk','irl']});
}
}
需要更改以适应上述代码的代码,我认为是导致错误的代码。正如此代码之前所做的那样,对于硬编码的选项值。但是现在js附加了新的输入字段,我不确定如何将其更改为适合。
function calculateAndDisplayRoute(directionsService, directionsDisplay) {
var waypts = [];
var checkboxArray = document.getElementById('dynamicInput');
for (var i = 0; i < checkboxArray.length; i++) {
if (checkboxArray.options[i].selected) {
waypts.push({
location: checkboxArray[i].value,
stopover: true
});
}
}