【问题标题】:Places search box inside bootstrap active tab content is not displaying引导活动选项卡内容内的位置搜索框未显示
【发布时间】:2015-07-09 06:56:23
【问题描述】:

我正在尝试在引导选项卡内容中显示 Google 地图。我可以在没有标签的情况下显示地图。当我将地图画布放在选项卡内容中时出现问题,地图不显示。

index.xhtml:

<ul id="tabs" class="nav nav-tabs" data-tabs="tabs">
    <li class="active"><a href="#red" data-toggle="tab">Red</a></li>
    <li><a href="#orange" data-toggle="tab">Orange</a></li>
    <li><a href="#yellow" data-toggle="tab">Yellow</a></li>
</ul>
<div id="my-tab-content" class="tab-content">
    <div class="tab-pane active" id="red">
        <h1>Red</h1>
        <input id="pac-input" class="controls" type="text" placeholder="Search Box"></input>
        <div id="map-canvas" ></div>

    </div>
    <div class="tab-pane" id="orange">
        <h1>Orange</h1>
        <p>orange orange orange orange orange</p>
    </div>
    <div class="tab-pane" id="yellow">
        <h1>Yellow</h1>
        <p>yellow yellow yellow yellow yellow</p>
    </div>

</div>

java_script:

function initialize() {

    var markers = [];
    var map = new google.maps.Map(document.getElementById('map-canvas'), {
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });

    var defaultBounds = new google.maps.LatLngBounds(
        new google.maps.LatLng(-33.8902, 151.1759),
        new google.maps.LatLng(-33.8474, 151.2631));
    map.fitBounds(defaultBounds);

    // Create the search box and link it to the UI element.
    var input = /** @type {HTMLInputElement} */(
        document.getElementById('pac-input'));
    map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);

    var searchBox = new google.maps.places.SearchBox(
      /** @type {HTMLInputElement} */(input));

    // Listen for the event fired when the user selects an item from the
    // pick list. Retrieve the matching places for that item.
    google.maps.event.addListener(searchBox, 'places_changed', function() {
      var places = searchBox.getPlaces();

      if (places.length == 0) {
        return;
      }
      for (var i = 0, marker; marker = markers[i]; i++) {
        marker.setMap(null);
      }

      // For each place, get the icon, place name, and location.
      markers = [];
      var bounds = new google.maps.LatLngBounds();
      for (var i = 0, place; place = places[i]; i++) {
        var image = {
          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.
        var marker = new google.maps.Marker({
          map: map,
          icon: image,
          title: place.name,
          position: place.geometry.location
        });

        markers.push(marker);

        bounds.extend(place.geometry.location);
      }

      map.fitBounds(bounds);
    });

    // Bias the SearchBox results towards places that are within the bounds of the
    // current map's viewport.
    google.maps.event.addListener(map, 'bounds_changed', function() {
      var bounds = map.getBounds();
      searchBox.setBounds(bounds);
    });
  }

  google.maps.event.addDomListener(window, 'load', initialize);

  function loadScript() {
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = 'https://maps.googleapis.com/maps/api/js?v=3.exp' +
        '&amp;signed_in=true&amp;callback=initialize';
    document.body.appendChild(script);
  }

  window.onload = loadScript;

文件:

<link href="bootstrap.min.css" rel="stylesheet"></link>
<link href="style.css" rel="stylesheet"></link>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script>
<script type="text/javascript" src="bootstrap.js"></script>

【问题讨论】:

    标签: javascript jquery html twitter-bootstrap


    【解决方案1】:

    我找到了问题,感谢@BalusC。在他编辑了我的问题后,我改进了搜索,发现:Google Map in Bootstrap Tab

    我笨拙的 css:

    #map-canvas{
        width: 100%;
        height: 100%;
        margin: 0px;
        padding: 0px
    }
    

    问题是父母的大小。应该是这样的:

    #map-canvas {
        width: 100px;
        height: 100px;
        margin: 0px;
        padding: 0px
    }
    

    而且我的 js 缺少事件触发器:

    google.maps.event.trigger(map, 'resize');

            $('a[href="#profile"]').on('shown', function(e) {
                google.maps.event.trigger(map, 'resize');
            });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-28
      • 1970-01-01
      • 2015-07-23
      • 1970-01-01
      • 1970-01-01
      • 2018-12-02
      • 2018-04-07
      相关资源
      最近更新 更多