Polygon
export var Polygon = Polyline.extend({
options: {
fill: true,
},
isEmpty: function () {
return !this._latlngs.length || !this._latlngs[0].length;
},
getCenter: function () {
if (!this._map) {
throw new Error("Must add layer to map before using getCenter()");
}
return PolyUtil.polygonCenter(this._defaultShape(), this._map.options.crs);
},
_convertLatLngs: function (latlngs) {
var result = Polyline.prototype._convertLatLngs.call(this, latlngs),
len = result.length;
if (
len >= 2 &&
result[0] instanceof LatLng &&
result[0].equals(result[len - 1])
) {
result.pop();
}
return result;
},
_setLatLngs: function (latlngs) {
Polyline.prototype._setLatLngs.call(this, latlngs);
if (LineUtil.isFlat(this._latlngs)) {
this._latlngs = [this._latlngs];
}
},
_defaultShape: function () {
return LineUtil.isFlat(this._latlngs[0])
? this._latlngs[0]
: this._latlngs[0][0];
},
_clipPoints: function () {
var bounds = this._renderer._bounds,
w = this.options.weight,
p = new Point(w, w);
bounds = new Bounds(bounds.min.subtract(p), bounds.max.add(p));
this._parts = [];
if (!this._pxBounds || !this._pxBounds.intersects(bounds)) {
return;
}
if (this.options.noClip) {
this._parts = this._rings;
return;
}
for (var i = 0, len = this._rings.length, clipped; i < len; i++) {
clipped = PolyUtil.clipPolygon(this._rings[i], bounds, true);
if (clipped.length) {
this._parts.push(clipped);
}
}
},
_updatePath: function () {
this._renderer._updatePoly(this, true);
},
_containsPoint: function (p) {
var inside = false,
part,
p1,
p2,
i,
j,
k,
len,
len2;
if (!this._pxBounds || !this._pxBounds.contains(p)) {
return false;
}
for (i = 0, len = this._parts.length; i < len; i++) {
part = this._parts[i];
for (j = 0, len2 = part.length, k = len2 - 1; j < len2; k = j++) {
p1 = part[j];
p2 = part[k];
if (
p1.y > p.y !== p2.y > p.y &&
p.x < ((p2.x - p1.x) * (p.y - p1.y)) / (p2.y - p1.y) + p1.x
) {
inside = !inside;
}
}
}
return inside || Polyline.prototype._containsPoint.call(this, p, true);
},
});
export function polygon(latlngs, options) {
return new Polygon(latlngs, options);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
编辑 (opens new window)
上次更新: 2025/04/15, 08:40:23