2017-04-20 121 views
0

代碼:如何在bootstrap選項卡中顯示地圖?

<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script> 
<script type="text/javascript"> 
    var directionsDisplay; 
    var directionsService = new google.maps.DirectionsService(); 
    var map; 

    function initialize() { 

     directionsDisplay = new google.maps.DirectionsRenderer(); 
     var chicago = new google.maps.LatLng(41.850033, -87.6500523); 
     var mapOptions = { 
      zoom: 7, 
      center: chicago 
     } 
     map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions); 
     directionsDisplay.setMap(map); 
    } 

    function calcRoute() { 
     var start = document.getElementById('start').value; 
     var end = document.getElementById('end').value; 
     var request = { 
      origin: start, 
      destination: end, 
      travelMode: google.maps.TravelMode.DRIVING 
     }; 
     directionsService.route(request, function (response, status) { 
      if (status == google.maps.DirectionsStatus.OK) { 
       directionsDisplay.setDirections(response); 
      } 
     }); 
    } 

    google.maps.event.addDomListener(window, 'load', initialize); 
</script> 

HTML代碼:

<div id="menu2" class="tab-pane fade"> 
    <div class="col-md-12"> 
     <p>Hello...</p> 
    </div> 
</div> 
<div id="menu2" class="tab-pane fade"> 
    <div class="col-md-12"> 
     <input type="text" placeholder="Source" name="start" id="start" value="noida"> 
     <input type="text" placeholder="Source" name="end" id="end" value="delhi"> 
     <input id="Button1" type="button" value="button" onclick="calcRoute()" /> 
     <div id="map-canvas"></div> 
    </div> 
</div> 

在該代碼中,我想要顯示其具有一個id = 「MENU2」 地圖沒有顯示的標籤內谷歌地圖。當我點擊id(menu1)時會顯示hello,但當我點擊id(menu2)文本字段正在呈現,但點擊按鈕時它不顯示地圖。我該如何解決這個問題?

謝謝

+0

你得到任何控制檯錯誤? –

+0

不,我沒有得到任何錯誤,但當我點擊按鈕時,只有線顯示沒有加載地圖。 – kevin

回答

0

我使用此代碼顯示在引導標籤谷歌地圖

的Html

<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 no_padding"> 
       <ul class="nav nav-tabs"> 
        <li class="active"><a data-toggle="tab" href="#overview">Overview</a></li> 
        <li><a data-toggle="tab" href="#host">The Host</a></li> 
        <li><a data-toggle="tab" href="#location">Location</a></li> 
        <li><a data-toggle="tab" href="#reviews">Reviews</a></li> 
       </ul> 
       <div class="tab-content"> 
        <div id="overview" class="tab-pane fade in active"> 

        </div> 
        <div id="host" class="tab-pane fade"> 

        </div> 
        <div id="location" class="tab-pane fade"> 
         <div id="map" style="width:100%;height:500px"></div> 
        </div> 
        <div id="reviews" class="tab-pane fade"> 

        </div> 
       </div> 
      </div> 

jQuery的

jQuery(document).ready(function() { 
    function myMap() { 
     var mapCanvas = document.getElementById("map"); 
     var myCenter = new google.maps.LatLng(11.0238913, 76.9934423); 
     var mapOptions = { 
      center: myCenter, 
      zoom: 14, 
      mapTypeControlOptions: { 
       mapTypeIds: [google.maps.MapTypeId.ROADMAP, google.maps.MapTypeId.HYBRID] 
      }, 
      mapTypeId: google.maps.MapTypeId.ROADMAP, 
      disableDefaultUI: true, 
      mapTypeControl: false, 
      scaleControl: true, 
      zoomControl: true, 
      zoomControlOptions: { 
       style: google.maps.ZoomControlStyle.LARGE, 
       position: google.maps.ControlPosition.LEFT_TOP 
      } 
     }; 

     var map = new google.maps.Map(mapCanvas, mapOptions); 
     map.setCenter(myCenter); 

     var marker = new google.maps.Marker({ 
      map: map, 
      position: myCenter, 
      draggable: true 
     }); 

     marker.setMap(map); 

     google.maps.event.addListener(marker, 'dragend', function() { 
      map.setCenter(this.getPosition()); // Set map center to marker position 
     }); 

     jQuery('.nav-tabs a[href="#location"]').on('shown.bs.tab', function() { 
      google.maps.event.trigger(map, 'resize'); 
      map.setCenter(myCenter); 
     }); 

    } 

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

}); 
+0

我的代碼Anandh Sp有什麼問題? – kevin

+0

檢查您是否在選項卡上使用相同的id(menu2)兩次 –

+0

否Anandh我檢查它 – kevin

相關問題