Google map API not displaying map on page, not sure why

Now it’s fixed, I think Twitter Bootstrap CSS should conflict with setting the height and width to 100%. Care for anyone who has a similar problem.

I am trying to implement a google map on my web page, when the user clicks the link, all PHP is working correctly, but for some reason the google map is not showing, I re-read all the examples in the API documents and re-read all the code, but still does not work sure why ..

Here is the map page.

Note. PHP is working fine and the script loads when the page loads, but the map is not.

<?php /* Include header and config and set variable*/ require_once('config.inc.php'); require_once($rootdir . $dirsubfolder . 'navbar.php'); $route = $_GET['route']; ?> <?php /* User wants to retrieve their route */ if (isset($route)) { ?> <?php /* User wants Route 1 */ if (strcmp($route, "sunroute1") == 0) { ?> <script type="text/javascript"> function initialize() { var mapOptions = { center: new google.maps.LatLng(-34.397, 150.644), zoom: 8, mapTypeId: google.maps.MapTypeId.ROADMAP }; var map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions); } function loadScript() { var script = document.createElement("script"); script.type = "text/javascript"; script.src = "http://maps.googleapis.com/maps/api/js?key=WORKINGKEYk&sensor=true&callback=initialize"; document.body.appendChild(script); } window.onload = loadScript; </script> <?php printf('worked'); } else { printf('failed'); } ?> <div class="container"> <div class="hero-unit"> <div id="map_canvas" style="width: 100%; height: 100%"></div> </div> </div> <?php /* End isset(route) */ } /* Include footer */ require_once($rootdir . $dirsubfolder . 'footer.php'); ?> 

and inside my footer.php I have

 <script src="https://maps.googleapis.com/maps/api/js?key=MYKEYWORKING&sensor=false"></script> 

Here is the view source from the page

 <script type="text/javascript"> function initialize() { var mapOptions = { center: new google.maps.LatLng(-34.397, 150.644), zoom: 8, mapTypeId: google.maps.MapTypeId.ROADMAP }; var map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions); } function loadScript() { var script = document.createElement("script"); script.type = "text/javascript"; script.src = "http://maps.googleapis.com/maps/api/js?key=WORKING&sensor=true&callback=initialize"; document.body.appendChild(script); } window.onload = loadScript; </script> worked <div class="container"> <div class="hero-unit"> <div id="map_canvas" style="width: 100%; height: 100%"></div> </div> </div> 

And my scripts in the footer

  <script src="http://code.jquery.com/jquery-latest.js"></script> <script src="js/bootstrap-transition.js"></script> <script src="js/bootstrap-alert.js"></script> <script src="js/bootstrap-modal.js"></script> <script src="js/bootstrap-dropdown.js"></script> <script src="js/bootstrap-scrollspy.js"></script> <script src="js/bootstrap-tab.js"></script> <script src="js/bootstrap-tooltip.js"></script> <script src="js/bootstrap-popover.js"></script> <script src="js/bootstrap-button.js"></script> <script src="js/bootstrap-collapse.js"></script> <script src="js/bootstrap-carousel.js"></script> <script src="js/bootstrap-typeahead.js"></script> <script src="https://maps.googleapis.com/maps/api/js?key=WORKING&sensor=false"></script> </body> </html> 
+4
source share
2 answers

Now it’s fixed, I think Twitter Bootstrap CSS should conflict with setting the height and width to 100%. Care for anyone who has a similar problem.

+2
source

You are correct that the problem was height:100% , but this is not a mysterious conflict with Bootstrap. You haven’t indicated your code enough to know exactly what is wrong, but you have seen and experienced similar problems many times, I have a pretty good idea what it would be like.

I suspect that at the time of initializing your map, the hero-unit element has not yet received a height or width, so its height is zero. Because of this, the map_canvas element also has a height of zero. In the end, 100% of zero is zero.

When you call new google.maps.Map() , it creates a map using the current height of the map_canvas element that you give it, i.e. zero. This is not so good.

Even if you later give the hero-unit height, this will not fix it. Of course, height:100% will now resize map_canvas to fit, but the Maps API does not know what happened!

You can fix this the same way you can by pointing your map_canvas element to an explicit height or, alternatively, making this call after resizing the element:

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

This tells the Maps API to look at the new element size and adjust accordingly. It is also a call to do if you ever want to resize a map: listen for the resize event on your map element and make this call when the event fires.

To illustrate, let's look at a very simplified version of your code with some log input:

 <!DOCTYPE html> <html> <head> <title>CSS Width and Height Test</title> </head> <body onload="test()"> <div id="container"> <div id="hero-unit"> <div id="map_canvas" style="width: 100%; height: 100%" ></div> </div> </div> <script> function test() { var hero = document.getElementById('hero-unit'); var canvas = document.getElementById('map_canvas'); console.log( 'Canvas size before setting hero size: ' + canvas.offsetWidth + 'x' + canvas.offsetHeight ); hero.style.height = '300px'; hero.style.width = '400px'; console.log( 'Canvas size after setting hero size: ' + canvas.offsetWidth + 'x' + canvas.offsetHeight ); } </script> </body> </html> 

Save this page and view it in your favorite browser with the developer tools console open so you can see the log messages. For example, in Chrome on Windows, use the F12 key to open the developer tools, then click the Console tab.

You should see something like this (the widths will depend on the width of your browser window):

 Canvas size before setting hero size: 1244x0 css-height.html:20 Canvas size after setting hero size: 400x300 css-height.html:29 

As you can see, up to the size of the parent element, the height of map_canvas is zero, and its width is greater than you expected.

Some related comments:

If you’re not used to using the tools of the browser developer, as we did, spend a little time and familiarize yourself with them. Chrome, in particular, has a fantastic set of tools in this dashboard. You can set a breakpoint in your JavaScript code, either by adding this operator anywhere:

 debugger; 

or by looking at the source file in the developer tools and clicking the left field. When the browser stops at a breakpoint, you can check JavaScript variables and all kinds of things. You can use the Elements tab on the developer toolbar to check your DOM elements at any time. For this error, you can find your map_canvas in the Elements element table tree and look at its computed styles, where you will find that the height is zero.

Also, save some trouble debugging the Maps API code by completely ignoring your PHP code. The Maps API is the JavaScript API. It does not see your PHP code; he doesn’t even know that you wrote your site in PHP. All he sees is the final HTML / CSS / JavaScript code that is delivered to the browser.

Finally, it looks like you are loading the Maps API twice, first with a built-in script, and then with an asynchronous method. Once upon a time a lot. I would stick with a built-in script for simplicity if you don't need asynchronous loading.

+1
source

All Articles