Set Google Maps Container DIV width and height 100%

后端 未结 12 902
遇见更好的自我
遇见更好的自我 2020-11-28 04:45

I loaded Google Maps API v3 and print Google Map in div. But when set width & height to 100% and auto I can\'t see the Map.

Here is HTML code snippe

相关标签:
12条回答
  • 2020-11-28 05:18

    Setting Map Container to position to relative do the trick. Here is HTML.

    <body>
        <!-- Map container -->
        <div id="map_canvas"></div>
    </body>
    

    And Simple CSS.

    <style>
    html, body, #map_canvas {
        width: 100%;
        height: 100%;
        margin: 0;
        padding: 0;
    }
    #map_canvas {
        position: relative;
    }
    </style>
    

    Tested on all browsers. Here is the Screenshot.

    enter image description here

    0 讨论(0)
  • 2020-11-28 05:18

    I struggled a lot to find the answer.

    You don't really need to do anything with body size. All you need to remove the inline style from the map code:

    <iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="https://maps.google.co.uk/maps?f=q&amp;source=s_q&amp;hl=en&amp;geocode=&amp;q=new+york&amp;aq=&amp;sll=53.546224,-2.106543&amp;sspn=0.02453,0.084543&amp;ie=UTF8&amp;hq=&amp;hnear=New+York,+United+States&amp;t=m&amp;z=10&amp;iwloc=A&amp;output=embed"></iframe><br /><small><a href="https://maps.google.co.uk/maps?f=q&amp;source=embed&amp;hl=en&amp;geocode=&amp;q=new+york&amp;aq=&amp;sll=53.546224,-2.106543&amp;sspn=0.02453,0.084543&amp;ie=UTF8&amp;hq=&amp;hnear=New+York,+United+States&amp;t=m&amp;z=10&amp;iwloc=A" style="color:#0000FF;text-align:left">View Larger Map</a></small>
    

    remove all the inline style and add class or ID and then style it the way you like.

    0 讨论(0)
  • 2020-11-28 05:20

    You have to set all parent containers to a 100% width if you want to cover the whole page with it. You have to set an absolute value at width and height for the #content div at the very least.

    body, html {
      height: 100%;
      width: 100%;
    }
    
    div#content {
      width: 100%; height: 100%;
    }
    
    0 讨论(0)
  • 2020-11-28 05:22

    This Work for me.

        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    <style type="text/css"> 
    
    #cont{
        position: relative;
        width: 300px;
        height: 300px;
    }
    #map_canvas{
        overflow: hidden;
        position: absolute;
        top: 0;
        right: 0;
        bottom: 0;
        left: 0;
    }
    </style> 
    <script type="text/javascript" src="http://maps.google.com/maps/api/js?key=APIKEY"></script>
    <script type="text/javascript">
    function initialize() {
        console.log("Initializing...");
      var latlng = new google.maps.LatLng(LAT, LNG);
        var myOptions = {
          zoom: 10,
          center: latlng,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var map = new google.maps.Map(document.getElementById("map_canvas"),
            myOptions);
    }
    </script>
    </head>
    
    <body onload="initialize()">
    <div id="cont">
    <div id="map_canvas" style="width: 100%; height: 100%;"></div>
    </div>
    </body>
    </html>
    
    0 讨论(0)
  • 2020-11-28 05:25

    If that div is the only thing on your page, set:

    body, html {
      height: 100%;
      width: 100%;
    }
    
    0 讨论(0)
  • 2020-11-28 05:32

    I just added inline style .
    <div id="map_canvas" style="width:750px;height:484px;"></div>
    And it worked for me .

    0 讨论(0)
提交回复
热议问题