如何在一个页面中的2个Google地图中添加搜索框?

时间:2016-05-05 09:50:13

标签: javascript google-maps google-maps-api-3

我有2张谷歌地图,我想在两张谷歌地图上放置搜索框 HTML:

<div id="map">
</div>
<div id="map2">
</div>

JS:

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);
      });


            }

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

0 个答案:

没有答案