Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
  <head>
<script src="https://code.jquery.com/jquery-2.1.4.js"></script>
<script src="https://code.jquery.com/jquery-1.7.2.js"></script>
    <title>Simple Map</title>
    <meta name="viewport" content="initial-scale=1.0">
    <meta charset="utf-8">
    <style>
      html, body {
        height: 100%;
        margin: 0;
        padding: 0;
      }
      #map {
        height: 100%;
      }
    </style>
  </head>
  <body>
    <div id="map"></div>
    <button id="fitBounds">Change Bounds</button>
    <button id="fitBounds2">Fit current bounds (no real change)</button>
    <script src="https://code.jquery.com/jquery-1.12.1.min.js"></script>
    <script src="https://maps.googleapis.com/maps/api/js"></script>
  </body>
</html>
 
#fitBounds {
  position:absolute;
  top:0;
  left:0;
  margin-top:80px;
  margin-left:20px;
}
#fitBounds2 {
  position:absolute;
  top:0;
  left:0;
  margin-top:100px;
  margin-left:20px;
}
 
var map;
map = new google.maps.Map(document.getElementById('map'), {
  center: {lat: -34.397, lng: 150.644},
  zoom: 8
});  
// setup initial bounds
var boundsCoords1 = [45.5983128, 8.9172776];
var boundsCoords2 = [45.4555729, 9.169236];
var staticCoords1 = google.maps.LatLng(45.5983128, 8.9172776);
var staticCoords2 = google.maps.LatLng(45.4555729, 9.169236);
// Button to change the bounds
$('#fitBounds').click(function() {
  // Run event handler for after things settle
  map.addListener('idle', function() {
      alert('change, now idle');
      google.maps.event.clearListeners(map, 'idle');
  });
  var bounds1 = new google.maps.LatLng(boundsCoords1[0] ,boundsCoords1[1]);
  var bounds2 = new google.maps.LatLng(boundsCoords2[0] ,boundsCoords2[1]);
  // Actually fit bounds
  var bounds = new google.maps.LatLngBounds(bounds1,bounds2);
  map.fitBounds(bounds);
  // Change bounds so another result next time
  boundsCoords1[0] = boundsCoords1[0]-0.1;
  boundsCoords1[1] = boundsCoords1[1]+0.1;
});
$('#fitBounds2').click(function() {
  map.addListener('bounds_changed', function() {
      alert('bounds change');
      google.maps.event.clearListeners(map, 'bounds_changed');
  });
  // Run event handler for after things settle
  var currentCenter = map.getCenter();
  // Run event handler for after things settle
  // Setting up a situation where bounds will never change properly
  var bounds = new google.maps.LatLngBounds(staticCoords1,staticCoords2);
  map.fitBounds(bounds);
  var newCenter = map.getCenter();
  if(currentCenter!==newCenter) {
    alert('bounds function fired!');
  }
});
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
tempranovapro
0viewers