Adding "equals" method to LatLngBounds using the preferred LatLng.equals method. Much cleaner.

This commit is contained in:
Jason Sanford 2012-02-14 09:29:29 -07:00
parent 553e1d5ef8
commit 22e2fba84b
2 changed files with 63 additions and 0 deletions

View File

@ -0,0 +1,55 @@
<!DOCTYPE html>
<html>
<head>
<title>Leaflet debug page</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<link rel="stylesheet" href="../../dist/leaflet.css" />
<!--[if lte IE 8]><link rel="stylesheet" href="../../dist/leaflet.ie.css" /><![endif]-->
<script src="../leaflet-include.js"></script>
</head>
<body>
<div id="map" style="width: 500px; height: 500px;"></div>
<button onclick="javascript:capture1();">Capture Bounds 1</button>
<button onclick="javascript:capture2();">Capture Bounds 2</button>
<button onclick="javascript:testBounds();">Test Bounds</button>
<script type="text/javascript">
var cloudmadeUrl = 'http://{s}.tile.cloudmade.com/BC9A493B41014CAABB98F0471D759707/997/256/{z}/{x}/{y}.png',
cloudmadeAttribution = 'Map data &copy; 2011 OpenStreetMap contributors, Imagery &copy; 2011 CloudMade',
cloudmade = new L.TileLayer(cloudmadeUrl, {maxZoom: 18, attribution: cloudmadeAttribution}),
bounds = new L.LatLngBounds(new L.LatLng(49.5, -11.3), new L.LatLng(61.2, 2.5));
var bounds1, bounds2;
var map = new L.Map('map', {
center: bounds.getCenter(),
zoom: 7,
layers: [cloudmade],
maxBounds: bounds
});
function capture1 () {
bounds1 = map.getBounds();
}
function capture2() {
bounds2 = map.getBounds();
}
function testBounds() {
if (!(bounds1 && bounds2)) {
alert("Bounds 1 and 2 have not been set");
return;
}
alert("Bounds 1 " + (bounds1.equals(bounds2) ? "equals": "does not equal") + " bounds 2.")
}
</script>
</body>
</html>

View File

@ -80,6 +80,14 @@ L.LatLngBounds = L.Class.extend({
var sw = this._southWest, var sw = this._southWest,
ne = this._northEast; ne = this._northEast;
return [sw.lng, sw.lat, ne.lng, ne.lat].join(','); return [sw.lng, sw.lat, ne.lng, ne.lat].join(',');
},
equals: function (/*LatLngBounds*/ bounds) {
var equals = false;
if (bounds !== null) {
equals = (this._southWest.equals(bounds.getSouthWest()) && this._northEast.equals(bounds.getNorthEast()));
}
return equals;
} }
}); });