Add holes support to L.Polygon.setLatLngs(). Fixes #1518

This commit is contained in:
Alexander Parshin 2013-10-13 20:17:12 +04:00
parent 2629c181d0
commit f5de36e229
2 changed files with 44 additions and 2 deletions

View File

@ -24,6 +24,21 @@ describe('Polygon', function() {
var polygon = new L.Polygon([]); var polygon = new L.Polygon([]);
expect(polygon.getLatLngs()).to.eql([]); expect(polygon.getLatLngs()).to.eql([]);
}); });
it("can be initialized with holes", function () {
var originalLatLngs = [
[ //external rink
[0, 10], [10, 10], [10, 0]
], [ //hole
[2, 3], [2, 4], [3, 4]
]
];
var polygon = new L.Polygon(originalLatLngs);
//getLatLngs() returns only external ring
expect(polygon.getLatLngs()).to.eql([L.latLng([0, 10]), L.latLng([10, 10]), L.latLng([10, 0])]);
})
}); });
describe("#setLatLngs", function () { describe("#setLatLngs", function () {
@ -40,6 +55,22 @@ describe('Polygon', function() {
expect(sourceLatLngs).to.eql(originalLatLngs); expect(sourceLatLngs).to.eql(originalLatLngs);
}); });
it("can be set external ring and holes", function() {
var latLngs = [
[ //external rink
[0, 10], [10, 10], [10, 0]
], [ //hole
[2, 3], [2, 4], [3, 4]
]
];
var polygon = new L.Polygon([]);
polygon.setLatLngs(latLngs);
//getLatLngs() returns only external ring
expect(polygon.getLatLngs()).to.eql([L.latLng([0, 10]), L.latLng([10, 10]), L.latLng([10, 0])]);
})
}); });
describe("#spliceLatLngs", function () { describe("#spliceLatLngs", function () {

View File

@ -8,10 +8,12 @@ L.Polygon = L.Polyline.extend({
}, },
initialize: function (latlngs, options) { initialize: function (latlngs, options) {
var i, len, hole;
L.Polyline.prototype.initialize.call(this, latlngs, options); L.Polyline.prototype.initialize.call(this, latlngs, options);
this._initWithHoles(latlngs);
},
_initWithHoles: function (latlngs) {
var i, len, hole;
if (latlngs && L.Util.isArray(latlngs[0]) && (typeof latlngs[0][0] !== 'number')) { if (latlngs && L.Util.isArray(latlngs[0]) && (typeof latlngs[0][0] !== 'number')) {
this._latlngs = this._convertLatLngs(latlngs[0]); this._latlngs = this._convertLatLngs(latlngs[0]);
this._holes = latlngs.slice(1); this._holes = latlngs.slice(1);
@ -52,6 +54,15 @@ L.Polygon = L.Polyline.extend({
} }
}, },
setLatLngs: function (latlngs) {
if (latlngs && L.Util.isArray(latlngs[0]) && (typeof latlngs[0][0] !== 'number')) {
this._initWithHoles(latlngs);
return this.redraw();
} else {
return L.Polyline.prototype.setLatLngs.call(this, latlngs);
}
},
_clipPoints: function () { _clipPoints: function () {
var points = this._originalPoints, var points = this._originalPoints,
newParts = []; newParts = [];