add margin to LatLngBounds.equals method (#5071)

* add margin to LatLngBounds equals method

* add tests
This commit is contained in:
Miguel Andrade 2017-02-02 07:55:30 -08:00 committed by Iván Sánchez Ortega
parent 53e90945aa
commit ad75456fa5
2 changed files with 13 additions and 5 deletions

View File

@ -75,6 +75,14 @@ describe('LatLngBounds', function () {
expect(a.equals([[14, 13], [30, 40]])).to.eql(false); expect(a.equals([[14, 13], [30, 40]])).to.eql(false);
expect(a.equals(null)).to.eql(false); expect(a.equals(null)).to.eql(false);
}); });
it("returns true if compared objects are equal within a certain margin", function () {
expect(a.equals([[15, 11], [29, 41]], 1)).to.eql(true);
});
it("returns false if compared objects are not equal within a certain margin", function () {
expect(a.equals([[15, 11], [29, 41]], 0.5)).to.eql(false);
});
}); });
describe('#isValid', function () { describe('#isValid', function () {

View File

@ -211,15 +211,15 @@ LatLngBounds.prototype = {
return [this.getWest(), this.getSouth(), this.getEast(), this.getNorth()].join(','); return [this.getWest(), this.getSouth(), this.getEast(), this.getNorth()].join(',');
}, },
// @method equals(otherBounds: LatLngBounds): Boolean // @method equals(otherBounds: LatLngBounds, maxMargin?: Number): Boolean
// Returns `true` if the rectangle is equivalent (within a small margin of error) to the given bounds. // Returns `true` if the rectangle is equivalent (within a small margin of error) to the given bounds. The margin of error can be overriden by setting `maxMargin` to a small number.
equals: function (bounds) { equals: function (bounds, maxMargin) {
if (!bounds) { return false; } if (!bounds) { return false; }
bounds = toLatLngBounds(bounds); bounds = toLatLngBounds(bounds);
return this._southWest.equals(bounds.getSouthWest()) && return this._southWest.equals(bounds.getSouthWest(), maxMargin) &&
this._northEast.equals(bounds.getNorthEast()); this._northEast.equals(bounds.getNorthEast(), maxMargin);
}, },
// @method isValid(): Boolean // @method isValid(): Boolean