Javascript 如何在一个页面中添加两个谷歌地图中的搜索框?

Javascript 如何在一个页面中添加两个谷歌地图中的搜索框?,javascript,google-maps,google-maps-api-3,Javascript,Google Maps,Google Maps Api 3,我有两个谷歌地图,我想把搜索框放在两个谷歌地图上 HTML: 这两个地图显示正确,但我想在两个地图上都添加一个搜索框。当我运行搜索时,它只出现在一张地图上。我的代码中有什么问题?请帮助我。如果可以帮助您,请勾选此项,谷歌文档中的搜索框请勾选此项,如果可以帮助您,请勾选此项,谷歌文档中的搜索框请勾选此项 <div id="map"> </div> <div id="map2"> </div> function initMap() {

我有两个谷歌地图,我想把搜索框放在两个谷歌地图上 HTML:

这两个地图显示正确,但我想在两个地图上都添加一个搜索框。当我运行搜索时,它只出现在一张地图上。我的代码中有什么问题?请帮助我。

如果可以帮助您,请勾选此项,谷歌文档中的搜索框请勾选此项,如果可以帮助您,请勾选此项,谷歌文档中的搜索框请勾选此项
<div id="map">
</div>
<div id="map2">
</div>
function initMap() {
        map = new google.maps.Map(document.getElementById('map'), {
          /* center: {lat: 31.615114, lng: 74.150065}, */
          center: {lat: 31.601043, lng: 74.169527},
          zoom: 12,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        });

        map2 = new google.maps.Map(document.getElementById('map2'), {
            /* center: {lat: 31.615114, lng: 74.150065}, */
            center: {lat: 31.601043, lng: 74.169527},
            zoom: 12,
            mapTypeId: google.maps.MapTypeId.ROADMAP
          });
 var input2 = document.getElementById('pac-input');
      var searchBox2 = new google.maps.places.SearchBox(input2);
      map2.controls[google.maps.ControlPosition.TOP_LEFT].push(input2);

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

      var markers2 = [];
      // Listen for the event fired when the user selects a prediction and retrieve
      // more details for that place.
      searchBox2.addListener('places_changed', function() {
        var places2 = searchBox2.getPlaces();

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

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

        // For each place, get the icon, name and location.
        var bounds2 = new google.maps.LatLngBounds();
        places.forEach(function(place) {
          var icon2 = {
            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.
          markers2.push(new google.maps.Marker({
            map: map2,
            icon: icon2,
            title: place.name,
            position: place.geometry.location
          }));

          if (place2.geometry.viewport) {
            // Only geocodes have viewport.
            bounds2.union(place2.geometry.viewport);
          } else {
            bounds2.extend(place212.geometry.location);
          }
        });
        map2.fitBounds(bounds2);
      });


        var input = document.getElementById('pac-input');
      var searchBox = new google.maps.places.SearchBox(input);
      map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);

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

      var markers = [];
      // Listen for the event fired when the user selects a prediction and retrieve
      // more details for that place.
      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) {
          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);
      });


            }