添加两个具有不同回调的谷歌地图

时间:2017-01-27 08:07:48

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

我的网页上有两个谷歌地图容器。

第一个 - id =" map"只是一个正常的显示地图" 第二个 - id =" map2"是一个搜索地图,用户在输入中键入,地图刷新到用户输入的位置。

这些地图使用相同的api密钥,但它们的回调不同。

如果我在控制台中添加以下两个get和error,说明重复的api调用

      <script async defer src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap" type="text/javascript"></script>
      <script async defer src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places&callback=initAutocomplete" type="text/javascript"></script>

但每个地图使用的回调都不同?

那么我如何才能让两张地图都能正常工作呢,因为目前我要么开始工作,要么另一张不能工作

我的代码如下:

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

    <script>
    function initMap() {
      var paarl = {lat: -33.725853, lng: 18.988628};
      var map = new google.maps.Map(document.getElementById('map'), {
        zoom: 15,
        scrollwheel:  false,
        center: paarl
      });
      var marker = new google.maps.Marker({
        position: paarl,
        map: map
      });
    }
  </script>

  <script>
  function initAutocomplete() {
    var map = new google.maps.Map(document.getElementById('map2'), {
      center: {lat: -33.8688, lng: 151.2195},
      zoom: 13,
      scrollwheel:  false,
      mapTypeId: 'roadmap'
    });

    // Create the search box and link it to the UI element.
    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) {
        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 async defer src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap" type="text/javascript"></script>
  <script async defer src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places&callback=initAutocomplete" type="text/javascript"></script>

1 个答案:

答案 0 :(得分:5)

不要使用两个谷歌地图库请求

最简单的方法是在第一个initMap中调用第二个函数

    <script>
      function initMap() {
        var paarl = {lat: -33.725853, lng: 18.988628};
        var map = new google.maps.Map(document.getElementById('map'), {
          zoom: 15,
          scrollwheel:  false,
          center: paarl
        });
        var marker = new google.maps.Marker({
          position: paarl,
          map: map
        });

      initAutocomplete();

      }


    </script>

或最终使用onload事件加载第二个