Javascript 谷歌地图API JS多个标记使用输入字段?

Javascript 谷歌地图API JS多个标记使用输入字段?,javascript,api,google-maps,google-maps-api-3,maps,Javascript,Api,Google Maps,Google Maps Api 3,Maps,我希望在网站上的搜索框中插入内容时能够显示多个标记。现在我有3个输入字段,我想增加。这就是我目前拥有的,我尝试在var中存储多个searchBox值,如下所示:var markers=searchBox.getPlaces(),searchBox.getPlaces1(),searchBox.getPlaces2() 如何将此代码扩展到其他输入字段 <input id="pac-input" class="controls" type="text" placeholder="Search

我希望在网站上的搜索框中插入内容时能够显示多个标记。现在我有3个输入字段,我想增加。这就是我目前拥有的,我尝试在
var
中存储多个searchBox值,如下所示:
var markers=searchBox.getPlaces(),searchBox.getPlaces1(),searchBox.getPlaces2()

如何将此代码扩展到其他输入字段

<input id="pac-input" class="controls" type="text" placeholder="Search Box" /><br />
<input id="pac-input1" class="controls" type="text" placeholder="Search Box" />
<input id="pac-input2" class="controls" type="text" placeholder="Search box" />
<div id="map"></div>
<script>
    function initAutocomplete() {
        var map = new google.maps.Map(document.getElementById('map'), {
            center: { lat: 52.728616, lng: 6.4901 },
            zoom: 13,
            mapTypeId: 'roadmap'
        });

        // Create the search box and link it to the UI element.
        var input = document.getElementById('pac-input');
        var input1 = document.getElementById('pac-input1');
        var input2 = document.getElementById('pac-input2');
        var searchBox = new google.maps.places.SearchBox(input);
        var searchBox1 = new google.maps.places.SearchBox(input1);
        var searchBox2 = new google.maps.places.SearchBox(input2);
        map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);
        map.controls[google.maps.ControlPosition.TOP_LEFT].push(input1);

        // Bias the SearchBox results towards current map's viewport.
        map.addListener('bounds_changed', function () {
            searchBox.setBounds(map.getBounds());
            searchBox1.setBounds(map.getBounds());
            searchBox2.setBounds(map.getBounds());
        });

        var markers = [];

        searchBox.addListener('places_changed', function () {
            var places = searchBox.getPlaces();

            if (places.length == 0) {
                return;
            }

            // Clear out the old markers.
            markers.forEach(function (marker) {
                marker.setMap(null);
            });
            markers = [];

            // For each place, get the icon, name and location.
            var bounds = new google.maps.LatLngBounds();
            places.forEach(function (place) {
                if (!place.geometry) {
                    console.log("Returned place contains no geometry");
                    return;
                }
                var icon = {
                    url: place.icon,
                    size: new google.maps.Size(71, 71),
                    origin: new google.maps.Point(0, 0),
                    anchor: new google.maps.Point(17, 34),
                    scaledSize: new google.maps.Size(25, 25)
                };

                // Create a marker for each place.
                markers.push(new google.maps.Marker({
                    map: map,
                    icon: icon,
                    title: place.name,
                    position: place.geometry.location
                }));

                if (place.geometry.viewport) {
                    // Only geocodes have viewport.
                    bounds.union(place.geometry.viewport);
                } else {
                    bounds.extend(place.geometry.location);
                }
            });
            map.fitBounds(bounds);
        });
    }

</script>
<script src="https://maps.googleapis.com/maps/api/js?key=[APIKEY]&libraries=places&callback=initAutocomplete"
        async defer></script>

函数initAutocomplete(){ var map=new google.maps.map(document.getElementById('map'){ 中心:{lat:52.728616,lng:6.4901}, 缩放:13, mapTypeId:“路线图” }); //创建搜索框并将其链接到UI元素。 var input=document.getElementById('pac-input'); var input1=document.getElementById('pac-input1'); var input2=document.getElementById('pac-input2'); var searchBox=newgoogle.maps.places.searchBox(输入); var searchBox1=新的google.maps.places.SearchBox(input1); var searchBox2=新的google.maps.places.SearchBox(input2); map.controls[google.maps.ControlPosition.TOP_LEFT].push(输入); map.controls[google.maps.ControlPosition.TOP\u LEFT].push(input1); //将搜索框结果偏向当前地图的视口。 map.addListener('bounds_changed',function(){ searchBox.setBounds(map.getBounds()); searchBox1.setBounds(map.getBounds()); searchBox2.setBounds(map.getBounds()); }); var标记=[]; searchBox.addListener('places\u changed',函数(){ var places=searchBox.getPlaces(); 如果(places.length==0){ 返回; } //清除旧的标记。 markers.forEach(函数(marker){ marker.setMap(空); }); 标记=[]; //对于每个位置,获取图标、名称和位置。 var bounds=new google.maps.LatLngBounds(); 地点。forEach(功能(地点){ 如果(!place.geometry){ log(“返回的位置不包含几何图形”); 返回; } 变量图标={ url:place.icon, 大小:新谷歌地图大小(71,71), 来源:新google.maps.Point(0,0), 主播:新google.maps.Point(17,34), scaledSize:new google.maps.Size(25,25) }; //为每个地方创建一个标记。 markers.push(新的google.maps.Marker)({ 地图:地图, 图标:图标, 标题:place.name, 位置:place.geometry.location })); if(place.geometry.viewport){ //只有地理代码具有视口。 联合(place.geometry.viewport); }否则{ 扩展(place.geometry.location); } }); 映射边界(bounds); }); }
相关问题(用于自动完成):

获取要用于
SearchBox
元素数组,使用这些元素创建
SearchBox
对象,创建一个函数,该函数使用
SearchBox
的唯一标识符和对
SearchBox
对象的引用。使用该函数可处理来自每个
搜索框
对象的事件

var searchBoxes = document.getElementsByClassName('controls');
for (var i=0; i<searchBoxes.length;i++) {
  var searchBox = new google.maps.places.SearchBox(searchBoxes[i]);
  map.controls[google.maps.ControlPosition.TOP_LEFT].push(searchBoxes[i]);
  map.addListener('bounds_changed', function() {
    searchBox.setBounds(map.getBounds());
  });
  markers.push([]);
  searchBox.addListener('places_changed', (function(i) {
    return function() {
      processSearch(i, this)
    }
  }(i)));
}

代码片段:

html,
身体,
#地图{
身高:100%;
宽度:100%;
边际:0px;
填充:0px
}
.控制{
宽度:100px;
}

函数initAutocomplete(){
var map=new google.maps.map(document.getElementById('map'){
中心:{
拉脱维亚:52.728616,
液化天然气:6.4901
},
缩放:13,
mapTypeId:“路线图”
});
var标记=[];
//创建搜索框并将其链接到UI元素。
var searchbox=document.getElementsByClassName('controls');
对于(var i=0;ifunction processSearch(uniqueId, searchBox) {
  var places = searchBox.getPlaces();

  if (places.length == 0) {
    return;
  }

  // Clear out the old markers.
  markers[uniqueId].forEach(function(marker) {
    marker.setMap(null);
  });
  markers[uniqueId] = [];

  // For each place, get the icon, name and location.
  var bounds = new google.maps.LatLngBounds();
  places.forEach(function(place) {
    if (!place.geometry) {
      console.log("Returned place contains no geometry");
      return;
    }
    var icon = {
      url: place.icon,
      size: new google.maps.Size(71, 71),
      origin: new google.maps.Point(0, 0),
      anchor: new google.maps.Point(17, 34),
      scaledSize: new google.maps.Size(25, 25)
    };

    // Create a marker for each place.
    if (!markers[uniqueId]) markers.push([]);
    markers[uniqueId].push(new google.maps.Marker({
      map: map,
      icon: icon,
      title: place.name,
      position: place.geometry.location
    }));

    if (place.geometry.viewport) {
      // Only geocodes have viewport.
      bounds.union(place.geometry.viewport);
    } else {
      bounds.extend(place.geometry.location);
    }
  });
  map.fitBounds(bounds);
}