Throw error on call to L.Polygon getCenter before map add (#4820)

* Throw error on call to L.Polygon getCenter before map add

References #4740

* add polyline handling, tests, and docstring
This commit is contained in:
Steve 2016-08-15 10:01:41 -05:00 committed by Iván Sánchez Ortega
parent 1a0b08226b
commit 92baa9c73f
4 changed files with 27 additions and 0 deletions

View File

@ -162,6 +162,16 @@ describe('Polygon', function () {
expect(layer.getCenter()).to.be.nearLatLng(L.latLng([0, 0]));
});
it('throws error if not yet added to map', function () {
expect(function () {
var latlngs = [
[[0, 0], [10, 0], [10, 10], [0, 10]]
];
var layer = new L.Polygon(latlngs);
var center = layer.getCenter();
}).to.throwException('Must add layer to map before using getCenter()');
});
});
describe("#_defaultShape", function () {

View File

@ -135,6 +135,13 @@ describe('Polyline', function () {
expect(polyline.getCenter()).to.be.nearLatLng(L.latLng([0, 0]), 1e-2);
});
it('throws error if not yet added to map', function () {
expect(function () {
var polyline = new L.Polyline([[0, 0], [0, 0.090]]);
var center = polyline.getCenter();
}).to.throwException('Must add layer to map before using getCenter()');
});
});
describe('#_flat', function () {

View File

@ -55,6 +55,11 @@ L.Polygon = L.Polyline.extend({
},
getCenter: function () {
// throws error when not yet added to map as this center calculation requires projected coordinates
if (!this._map) {
throw new Error('Must add layer to map before using getCenter()');
}
var i, j, p1, p2, f, area, x, y, center,
points = this._rings[0],
len = points.length;

View File

@ -105,6 +105,11 @@ L.Polyline = L.Path.extend({
// @method getCenter(): LatLng
// Returns the center ([centroid](http://en.wikipedia.org/wiki/Centroid)) of the polyline.
getCenter: function () {
// throws error when not yet added to map as this center calculation requires projected coordinates
if (!this._map) {
throw new Error('Must add layer to map before using getCenter()');
}
var i, halfDist, segDist, dist, p1, p2, ratio,
points = this._rings[0],
len = points.length;