Google使用文本输入数组自动填充路径点

时间:2016-11-01 16:11:57

标签: javascript arrays google-maps

我为一个名为waypoints[]的文本输入构建了一个带动态数组的表单。我需要提供表单文本输入访问我的Google地方信息自动填充功能(已经适用于我的非数组文本输入)。

这就是我所拥有的:

JavaScript的:

function initialize() {
    var options = {
        componentRestrictions: {
           country: "au"
        }
    };

    var inputs = document.getElementById('start');
    var autocompletes = new google.maps.places.Autocomplete(inputs, options);
    var inpute = document.getElementById('end');
    var autocompletee = new google.maps.places.Autocomplete(inpute, options);

    for (var i = 0; i < waypoints.length; i++) {
        var inputw = document.getElementById(waypoints[i]);
        var autocompletew = new google.maps.places.Autocomplete(inputw, options);
    }

    directionsDisplay = new google.maps.DirectionsRenderer();
    var melbourne = new google.maps.LatLng(-31.953512, 115.857048);
    var myOptions = {
        zoom: 12,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        center: melbourne
    }

    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    directionsDisplay.setMap(map);
}

HTML

<div id="dynamicInput">
      <br>1<input type="text" name="waypoints[]" autocomplete="on">
</div>
<input type="button" value="Another Delivery" onClick="addInput('dynamicInput');">

实际上,&#34;开始&#34;和&#34;结束&#34;工作正常,但waypoints[]没有。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

航点的<input>没有ID,您无法通过document.getElementById获取对它的引用。您需要执行以下操作:

var waypoints = document.getElementsByName("waypoints[]");
for (var i = 0; i < waypoints.length; i++) {
    var inputw = waypoints[i];
    var autocompletew = new google.maps.places.Autocomplete(inputw, options);
}

proof of concept fiddle