18

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 Google Map API. When a button is clicked, the Reveal Modal pops up, and displays the Google Map. My problem is that only a third of the map is displaying. This is because a resize trigger needs to be implemented. My question is how do I implement the google.maps.event.trigger(map, 'resize')? Where do I place this little snippet of code?

Here is my test site. Click on the Google Map button to see the problem at hand. http://simplicitdesignanddevelopment.com/Fannie%20E%20Zurb/foundation/contact_us.html#

The Reveal Model script:

<script type="text/javascript">
  $(document).ready(function() {
  $('#myModal1').click(function() {
  $('#myModal').reveal();
       });
          });
 </script>

My Google Map Script:

<script type="text/javascript">
 function initialize() {
 var mapOptions = {
 center: new google.maps.LatLng(39.739318, -89.266507),
 zoom: 5,
 mapTypeId: google.maps.MapTypeId.ROADMAP
 };
 var map = new google.maps.Map(document.getElementById("map_canvas"),
 mapOptions);
 }

 </script>

The div which holds the Google Map:

  <div id="myModal" class="reveal-modal large">
  <h2>How to get here</h2>
  <div id="map_canvas" style="width:600px; height:300px;"></div>
  <a class="close-reveal-modal">&#215;</a>
 </div>

UPDATE 2018-05-22

With a new renderer release in version 3.32 of Maps JavaScript API the resize event is no longer a part of Map class.

The documentation states

When the map is resized, the map center is fixed

  • The full-screen control now preserves center.

  • There is no longer any need to trigger the resize event manually.

source: https://developers.google.com/maps/documentation/javascript/new-renderer

google.maps.event.trigger(map, "resize"); doesn't have any effect starting from version 3.32

Community
  • 1
  • 1
Jeremy Flaugher
  • 417
  • 2
  • 8
  • 15
  • 1
    This is something weird I haven't seen before. I opened your site and as you said the map isn't shown completely. However if I open the chrome console using F12 or the firebug in mozilla, immediately your map is shown completely and until again I refresh the page I get the complete map. – Shiridish Oct 25 '12 at 04:19
  • I dont have a solution to your question, but found this interesting. +1. – Shiridish Oct 25 '12 at 04:20
  • Thank you for the +1. I have tried adding an js event, buy haven't gotten anything to work. Someone out there has to know what is going on. – Jeremy Flaugher Oct 25 '12 at 04:46

5 Answers5

20

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!

MarcoK
  • 6,090
  • 2
  • 28
  • 40
  • 2
    Thank you very much for your time and very detailed working example @MarcoK. You hit everything on the head and my problem is now fixed. I will remember to provide a jsfiddle next time I am on here. Thanks again!! – Jeremy Flaugher Oct 25 '12 at 17:30
  • 5
    Hm, the solution is gone now... would have been great to have this on JSFiddle as well... – bfncs Sep 29 '13 at 14:08
  • @MarcoK you provided 'Download the solution' link is broken and not working. it shows an error message like 'Error (404) We can't find the page you're looking for.' Kindly update the link or remove this link from this stackoverflow post. Thanks. – Kamlesh Sep 26 '20 at 05:33
11

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>
Cristiano Fontes
  • 4,920
  • 6
  • 43
  • 76
  • Thank you for your reply, tho I could not get it to work. I changed my code just as you said. I am still just getting a third of the map. – Jeremy Flaugher Oct 24 '12 at 23:45
  • 1
    Still haven't gotten this figured out. Grrrrrrrrrr – Jeremy Flaugher Oct 25 '12 at 01:58
  • I see there is an error with your Jquery, look `Uncaught ReferenceError: $ is not defined ` and also there is a `;` missing `after (map, 'resize')` – Cristiano Fontes Oct 25 '12 at 09:26
  • Yes, for something that was supposed to be cut and paste I sure did mess the heck out of it all. You were right about your whole answer, I just didn't know how to correctly change and place the the corrections. Thanks to helpful people like yourself, JS noobs like myself get to learn and be hands on. Stackoverflow.com is a forehead-saver! Thanks so much for your time and effort @Crisiano Fontes! – Jeremy Flaugher Oct 25 '12 at 17:33
5
google.maps.event.addListenerOnce(map, 'tilesloaded', function() {
                google.maps.event.addListenerOnce(map, 'tilesloaded', function() {
                    google.maps.event.trigger(map, 'resize');
                });
});
3

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>
Andrey Davis
  • 31
  • 1
  • 1
2

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.

  • Did you ever figure this out? I'm having the same issue and all I'm finding is the google.maps.event.trigger(map, 'resize'); stuff everywhere – Ted Oct 06 '20 at 15:20
  • Nope we're still thinking about ways of how to do it properly. This is the last of our bugs so when we find better way I'll answer here. Also thing about triggering resize is now working so we did something like: parentElement.resize(99%); parentElement.resize(100%); And it's the only was it worked – Ilya Semenov Oct 07 '20 at 17:13