Google Maps API Re-size

107 views Asked by At

Completly new to Google Maps API. At the moment I have it pulling in data from my mySQL database and adding in a marker.

However I want to change the size of the map to fit my sites layout. This is the code I have at the moment.

<!DOCTYPE html >
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <title>PHP/MySQL & Google Maps Example</title>
    <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js"></script>
    <script type="text/javascript">
    //<![CDATA[

    var customIcons = {
      restaurant: {
        icon: 'http://labs.google.com/ridefinder/images/mm_20_blue.png'
      },
      bar: {
        icon: 'http://labs.google.com/ridefinder/images/mm_20_red.png'
      }
    };

    function load() {
      var map = new google.maps.Map(document.getElementById("map"), {
        center: new google.maps.LatLng(0, 0),
        zoom: 1,
        mapTypeId: 'roadmap'
      });
      var infoWindow = new google.maps.InfoWindow;

      // Change this depending on the name of your PHP file
      downloadUrl("phpsqlajax_genxml.php", function(data) {
        var xml = data.responseXML;
        var markers = xml.documentElement.getElementsByTagName("marker");
        for (var i = 0; i < markers.length; i++) {
          var name = markers[i].getAttribute("name");
          var address = markers[i].getAttribute("address");
          var type = markers[i].getAttribute("type");
          var point = new google.maps.LatLng(
              parseFloat(markers[i].getAttribute("lat")),
              parseFloat(markers[i].getAttribute("lng")));
          var html = "<b>" + name + "</b> <br/>" + address;
          var icon = customIcons[type] || {};
          var marker = new google.maps.Marker({
            map: map,
            position: point,
            icon: icon.icon
          });
          bindInfoWindow(marker, map, infoWindow, html);
        }
      });
    }

    function bindInfoWindow(marker, map, infoWindow, html) {
      google.maps.event.addListener(marker, 'click', function() {
        infoWindow.setContent(html);
        infoWindow.open(map, marker);
      });
    }

    function downloadUrl(url, callback) {
      var request = window.ActiveXObject ?
          new ActiveXObject('Microsoft.XMLHTTP') :
          new XMLHttpRequest;

      request.onreadystatechange = function() {
        if (request.readyState == 4) {
          request.onreadystatechange = doNothing;
          callback(request, request.status);
        }
      };

      request.open('GET', url, true);
      request.send(null);
    }

    function doNothing() {}

    //]]>

  </script>

  </head>

  <body onload="load()">
    <div id="map" style="width: 600px; height: 450px"></div>
  </body>
  <script>
    google.maps.event.trigger(map, 'resize');
  </script>

</html>

I have done some reading and looked through the development documentation and seen that I should be using

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

However I can't figure out where this needs to go as it either breaks the map or does not re-size the map to required size.

Any thoughts?

1

There are 1 answers

2
Alex Gill On BEST ANSWER

The problem you have is that you have no access (scope) to the 'map' variable because you have declared this inside a function load().

Refactor your code! Theres a number of ways you can do this, a simple way would be to declare it as a global variable.

var map = blah blah...

function load() {
//...

JS:

window.onresize = function() {
  var currCenter = map.getCenter();
  google.maps.event.trigger(map, 'resize');
  map.setCenter(currCenter);
};

Example: http://codepen.io/alexgill/pen/NqjMma