简体   繁体   English

谷歌地图没有填写模态弹出窗口

[英]Google map not filling modal popup

I'm using a modal popup when users click the map on this page here , which brings up a larger map with directions. 当用户在此处单击此页面上的地图时,我正在使用模态弹出窗口,这会显示带有路线的更大地图。

The map isn't filling the entire space however and I can't figure out why. 然而地图并没有填满整个空间,我无法弄清楚原因。 When I right click on it to check in an the chrome inspector, it makes the map fill the modal. 当我右键单击它以检查chrome检查器时,它会使地图​​填充模态。

Excuse all the code, but I thought it best to include it all: 请原谅所有代码,但我认为最好将其全部包括在内:

       <div class="map clearfix">
            <div class="bg left-contact">
            <% if len(""&rsAdvert("ContactPostcode"))>0 then %>
                      <a href="#myModal" role="button" data-toggle="modal">
<div class="small-map" style="width:100%;height:130px;background:url(http://maps.google.com/maps/api/staticmap?center=<%=server.URLEncode(""&rsAdvert("ContactPostcode"))%>&zoom=14&size=250x250&maptype=roadmap&markers=color:ORANGE|label:A|<%=server.URLEncode(""&rsAdvert("ContactPostcode"))%>&sensor=false) center no-repeat;"></div></a>
                      <div class="expand"></div>
    <!-- Modal -->
    <div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
        <h3 id="myModalLabel">Modal header</h3>
      </div>
      <div class="modal-body">
        <p><div id='map-canvas'></div></p>
      </div>
      <div class="modal-footer">
        <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
        <button class="btn btn-primary">Save changes</button>
      </div>
    </div>
        <script src="http://code.jquery.com/jquery.js"></script>
        <script src="/js/bootstrap/js/bootstrap.min.js"></script>
               <% end if %> 
            </div>
        </div>

     <script src="http://maps.googleapis.com/maps/api/js?q=London&key=AIzaSyBaPEDyFbbnWjtvT8W3UBOM34Y7g6vK69A&sensor=false"></script>
    <script>
        var map;
        function initialize() {
          var mapOptions = {
            zoom: 15,
            center: new google.maps.LatLng(-34.397, 150.644),
            mapTypeId: google.maps.MapTypeId.ROADMAP
          };
          var geolocate = function(address, callback) {
                $.ajax({
                        url: "http://maps.googleapis.com/maps/api/geocode/json",
                        data: {
                            "sensor": true,
                            "address": address
                        },
                        dataType: "json",
                        success: function(d) {
                            if (d.status == "ZERO_RESULTS") callback(false);
                            if (d.results && d.results[0] && d.results[0].geometry) {
                                callback({
                                    "ne": d.results[0].geometry.bounds.northeast,
                                    "sw": d.results[0].geometry.bounds.southwest,
                                    "center": d.results[0].geometry.location
                                });
                            }
                            else callback(false);
                        }
                    });
          };
          map = new google.maps.Map(document.getElementById('map-canvas'),
              mapOptions);
          geolocate("<%=server.URLEncode(""&rs("ca_postcode"))%>", function(c) {
                map.setCenter(new google.maps.LatLng(c.center.lat, c.center.lng));
         });
        }
        google.maps.event.addDomListener(window, 'load', initialize);
    </script>

Thanks. 谢谢。

For completeness: if you're using Bootstrap modal window you need to use shown.bs.modal event in place of shown event. 为了完整性:如果您使用的是Bootstrap模态窗口 ,则需要使用shown.bs.modal事件代替shown事件。

Here the code: 这里的代码:

  $("#modalWindowId").on("shown.bs.modal", function(e) {
    google.maps.event.trigger(map, "resize");
    map.setCenter(markerLatLng); // Set here center map coordinates
  });

Take a look here for learn more: http://coderpills.wordpress.com/2014/06/02/showing-google-map-inside-a-bootstrap-modal-window/ 看看这里了解更多信息: http//coderpills.wordpress.com/2014/06/02/showing-google-map-inside-a-bootstrap-modal-window/

You can trigger a resize event on the map, after the modal is launched, which will cause it to resize to fit the dimensions of the containing div . 在模式启动后,您可以在地图上触发resize事件,这将使其调整大小以适合包含div的尺寸。 From the documentation : 文档

Developers should trigger this event on the map when the div changes size 当div改变大小时,开发人员应该在地图上触发此事件

// Add this at the bottom of the script which defines your map
// It will trigger the resize event on the map after the modal has launched
$('#myModal').on('shown', function () {
  google.maps.event.trigger(map, 'resize');
})

I think when you launch dev tools the window.onResize event is causing the resize event to be triggered on the map. 我认为当你启动dev工具时, window.onResize事件会导致在地图上触发resize事件。 You can prove this by detaching dev tools from the browser window before clicking your modal. 您可以通过在单击模式之前从浏览器窗口中分离开发工具来证明这一点。

Try to use this 试着用这个

padding: 0; 填充:0;

Css: CSS:

.modal-body {
max-height: 400px;
overflow-y: auto;
padding: 0;
position: relative;
     }

Thanks for the answers guys. 谢谢你的回答。 Just to help anyone else that may have this problem, in addition to having to resize due to having the map in a modal, I also had to reset the center. 只是为了帮助其他可能有这个问题的人,除了由于将地图放在模态中而不得不调整大小,我还必须重置中心。

The below code needed to be added within the initialize function. 以下代码需要在initialize函数中添加。

    $('#show_map').on('shown', function () {
        google.maps.event.trigger(map, "resize");
        map.setCenter(myLatlng);
    });
$('#Map_Modal_ID').on('shown.bs.modal', function () {
    initialize();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM