Note that there are some explanatory texts on larger screens.

plurals
  1. POGoogle Maps map.getBounds() immediately after a call to map.fitBounds
    text
    copied!<p>I'm calling getBounds() directly after a call to fitBounds(), and I thought I'd get a valid bound back as the map recenters and zooms to fit the bounds. Unfortunately, getBounds() is returning nil.</p> <p>The code to reproduce this is as follows:</p> <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml"&gt; &lt;head&gt; &lt;meta http-equiv="content-type" content="text/html; charset=utf-8"/&gt; &lt;script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"&gt;&lt;/script&gt; &lt;style&gt; #map { width: 800px; height: 400px; } &lt;/style&gt; &lt;/head&gt; &lt;body&gt; &lt;div id='map'&gt;&lt;/div&gt; &lt;script&gt; var myLatlng1 = new google.maps.LatLng(-38.397, 150.644); var myLatlng2 = new google.maps.LatLng(-34.897, 150.844); var myLatLngBounds = new google.maps.LatLngBounds(myLatlng1, myLatlng2); var myOptions = { mapTypeId: google.maps.MapTypeId.ROADMAP, center: new google.maps.LatLng(0, 0), zoom: 0 } var map = new google.maps.Map(document.getElementById("map"), myOptions); map.fitBounds(myLatLngBounds); console.log(map.getMapTypeId()); console.log(map.getZoom()); console.log(map.getBounds()); &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Is there something I'm missing? I haven't been able to find anything in the docs about this situation. The nearest I get is a note on getBounds that says:</p> <blockquote> <p>If the map is not yet initialized (i.e. the mapType is still null), or center and zoom have not been set then the result is null.</p> </blockquote> <p>Note that getZoom returns undefined, too. Does fitBounds() not set this value?</p> <p><strong>EDIT</strong> I've updated the code with a default zoom and center, as per Marcelo's suggestions.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload