Google使用输入字段映射API JS多个标记?

时间:2018-06-12 07:09:29

标签: 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 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>

1 个答案:

答案 0 :(得分:0)

相关问题(适用于自动填充):Google Maps API autocomplete 2nd address fields on same page

获取要用于<input>的{​​{1}}元素数组,使用这些元素创建SearchBox个对象,创建一个获取{{1}唯一标识符的函数和SearchBox对象的引用。使用该函数处理来自每个SearchBox对象的事件。

SearchBox

processSearch功能:

SearchBox

proof of concept fiddle

screenshot of resulting map

代码段

&#13;
&#13;
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)));
}
&#13;
function 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);
}
&#13;
&#13;
&#13;