6

I am having some difficulty with a google map. The problem is that only a small portion of the map is loading as shown here:

enter image description here

After the page loads, if I resize the browser even the slightest amount, this causes the complete map to refresh and load correctly, as shown here: enter image description here

Here is my javascript code:

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

$(document).ready(function(){                               
    var $width = document.getElementById("propertysection").offsetWidth;
    $('#map-canvas-2').width($width-28-175);
    $('#map-canvas-2').height($width);
    $('#myGridMap').height($width);
});

function initialize() { 
    var map; 
    var propertyMap;
    var marker; 
    var infowindow;
        var myOptions = {
            zoom: 6,
            center:new google.maps.LatLng(50.7,-86.05),
            mapTypeId: google.maps.MapTypeId.HYBRID
        }
        map = new google.maps.Map(document.getElementById("map-canvas-2"),myOptions);                  
        infowindow = new google.maps.InfoWindow({
            content: 'Property Info',
            position: new google.maps.LatLng(0, 0)
        }); 
    } 

Can anyone suggest what the problem might be? Thanks.

DanielAttard
  • 3,467
  • 9
  • 55
  • 104

2 Answers2

5

You shouldn't mix the jquery ready method with the window load event (see http://api.jquery.com/ready/).

Instead, call initialize from within your .ready function, or place the window resize functions in the initialize method itself (before initializing the map).

jlivni
  • 4,759
  • 1
  • 19
  • 30
0

I am using bootstrap and a mega menu and I resolved it with using the official Google Maps API asynchronous load function. https://developers.google.com/maps/documentation/javascript/tutorial#asynch

   function initialize() {

        var myLatLng = new google.maps.LatLng(37.4221147, -122.0867373);

        var map_canvas = document.getElementById('map_canvas');

        var map_options = {
            center: new google.maps.LatLng(37.4221147, -122.0867373),
            zoom: 12,
                    mapTypeControl: false,
                    panControl: false,
            zoomControlOptions: {
                        position: google.maps.ControlPosition.LEFT_CENTER
            },
            streetViewControl: false,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        }

        var map = new google.maps.Map(map_canvas, map_options);

        var contentString =
            '<b>Google Gate Bridge</b>' +
            '<p>Mountain View, CA 94043, USA</p>';

        var infowindow = new google.maps.InfoWindow({
            content: contentString
        });

        var marker = new google.maps.Marker({
            position: myLatLng,
            map: map,
        });

        google.maps.event.addListener(marker, 'click', function(){
            infowindow.open(map,marker);
        });

    }

    function loadScript() {
      var script = document.createElement("script");
      script.type = "text/javascript";
      script.src = "https://maps.googleapis.com/maps/api/js?sensor=false&callback=initialize";
      document.body.appendChild(script);
    }

    var tab = document.getElementById('YourHtmlObjectID');
    YourHtmlObjectID.onmouseover = loadScript;
    YourHtmlObjectID.onclick = loadScript;
florian
  • 23
  • 2