How to use google.maps.event.trigger(map, 'resize')

前端 未结 5 1586
無奈伤痛
無奈伤痛 2020-11-28 12:49

I am new to JS, and have found the answer to a previous question, which brought up a new question, which brought me here again.

I have a Reveal Modal that contains a

相关标签:
5条回答
  • 2020-11-28 13:25

    Didn't found how to leave a comment for the last answer, but +1 for Cristiano Fontes help! It worked. In my case:

    <script type="text/javascript">    
    
    $('div.map-box').hide();
    $('li.map').mouseover(function(){
        $('div.map-box').show();
        google.maps.event.trigger(map, 'resize');
    });
    
    </script>
    
    0 讨论(0)
  • 2020-11-28 13:27

    As now, google.maps.event.trigger(map, 'resize') does nothing. Like we having mat-dialog containing google map in it and there is the same problem. I`ve found some ways you can handle it:

    1. Init map with a timeout.
    2. Change the size of the parent container after map inits and then change it back.

    But as for me, all these ways are pretty bad and I`m looking for something better.

    0 讨论(0)
  • 2020-11-28 13:31
    google.maps.event.addListenerOnce(map, 'tilesloaded', function() {
                    google.maps.event.addListenerOnce(map, 'tilesloaded', function() {
                        google.maps.event.trigger(map, 'resize');
                    });
    });
    
    0 讨论(0)
  • 2020-11-28 13:38

    There were actually a couple of problems with your source code.

    • The initialize() function is created, but never called
    • The $(document).ready should be called after jQuery us loaded
    • The map should be a global variable (like @Cristiano Fontes said) and not a var map
    • (Important) The click event is never called. You're trying to combine the two methods Reveal from Zurb provides to open a dialog (one with JS, one with only HTML). You need to use the only JS method.
    • You're using the wrong ID (#myModal1 is never located in the HTML).

    And now: Download the solution (Please provide us with a download/JSFiddle next time, so we don't need to create this ourselves).

    Hope it helped!

    0 讨论(0)
  • 2020-11-28 13:38

    Just add it here

    <script type="text/javascript">
      $(document).ready(function() {
      $('#myModal1').click(function() {
      $('#myModal').reveal();
      google.maps.event.trigger(map, 'resize');
           });
              });
     </script>
    

    BUT you need to put the map as a global variable, so lose the var here

    <script type="text/javascript">
       function initialize() {
         var mapOptions = {
          center: new google.maps.LatLng(39.739318, -89.266507),
          zoom: 5,
          mapTypeId: google.maps.MapTypeId.ROADMAP
         };
     --> map = new google.maps.Map(document.getElementById("map_canvas"),
       mapOptions);
     }
    
     </script>
    
    0 讨论(0)
提交回复
热议问题