<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
Keyboard Shortcuts
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + ] | Indents selected lines |
ctrl + [ | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |