Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As the other answers suggested, the <a href="http://code.google.com/apis/maps/documentation/javascript/reference.html#Map" rel="noreferrer"><code>fitBounds()</code></a> method should do the trick.</p> <p>Consider the following example, which will generate 10 random points on the North East USA, and applies the <code>fitBounds()</code> method:</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;meta http-equiv="content-type" content="text/html; charset=UTF-8"/&gt; &lt;title&gt;Google Maps LatLngBounds.extend() Demo&lt;/title&gt; &lt;script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript"&gt;&lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;div id="map" style="width: 400px; height: 300px;"&gt;&lt;/div&gt; &lt;script type="text/javascript"&gt; var map = new google.maps.Map(document.getElementById('map'), { mapTypeId: google.maps.MapTypeId.TERRAIN }); var markerBounds = new google.maps.LatLngBounds(); var randomPoint, i; for (i = 0; i &lt; 10; i++) { // Generate 10 random points within North East USA randomPoint = new google.maps.LatLng( 39.00 + (Math.random() - 0.5) * 20, -77.00 + (Math.random() - 0.5) * 20); // Draw a marker for each random point new google.maps.Marker({ position: randomPoint, map: map }); // Extend markerBounds with each random point. markerBounds.extend(randomPoint); } // At the end markerBounds will be the smallest bounding box to contain // our 10 random points // Finally we can call the Map.fitBounds() method to set the map to fit // our markerBounds map.fitBounds(markerBounds); &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Refreshing this example many times, no marker ever goes outside the viewport:</p> <p><img src="https://i.stack.imgur.com/npt9Z.jpg" alt="fitBounds demo"></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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