Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

height=100% not rendering the google maps api

I am trying to use the Google Maps API. I have gotten a authorization key and pasted in the sample code they gave us, but I can't make the map render even when they tell me to make sure I put a height of 100% on both my map div and the body.

I have tried and it won't show up. If I put a 500px height on the map div, and give the body a 100% height it works... but I want to know why height of 100% will not will not render. I am copying exactly what the Google documentation (https://developers.google.com/maps/documentation/javascript/examples/map-simple) is giving me and what the video tells me to double check. HELP, i am insanely curious on why 100% height won't render. I want to use percentages, so that it can be compatible on mobile devices!

<!doctype html>
<html lang="en-us">
<head>
    <meta charset="utf-8">
    <title>Ice Cream</title>

        <!-- Google Docs-->


    <!-- external CSS link -->
    <link rel="stylesheet" href="css/reset.css">
    <link rel="stylesheet" href="css/style.css">
    <script src="http://code.jquery.com/jquery-latest.min.js"></script>


    </head>


<body>


<div id='map_canvas'> HELLOooo <!--dimensions of map are set in CSS-->
</div>


    <!-- external CSS link -->
    <!-- javascript -->
        <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDuDCh7Odrr4RUUiAHMyDtfwC2NtySy-64"></script>
        <script src="js/index.js"></script>



</body>
</html>


body, #map_canvas{
    margin:0;
    padding:0;
    height: 100%;



}


  $(function() {
    console.log("hi");

   function initialize() {
        var mapOptions = {
          center: { lat: -34.397, lng: 150.644},
          zoom: 8
        };
        var map = new google.maps.Map(document.getElementById('map_canvas'),
            mapOptions);
      }

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

console.log('map');



  });
like image 343
yasmin Avatar asked Feb 06 '26 15:02

yasmin


1 Answers

The browser needs to be able to calculate a non-zero height. You need to set the height of the html element to 100% also:

  $(function() {
    console.log("hi");

   function initialize() {
        var mapOptions = {
          center: { lat: -34.397, lng: 150.644},
          zoom: 8
        };
        var map = new google.maps.Map(document.getElementById('map_canvas'),
            mapOptions);
      }

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

console.log('map');



  });
html, body, #map_canvas{
    margin:0;
    padding:0;
    height: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id='map_canvas'> HELLOooo <!--dimensions of map are set in CSS-->
</div>
like image 74
geocodezip Avatar answered Feb 09 '26 09:02

geocodezip



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!