Circle
export var Circle = CircleMarker.extend({
initialize: function (latlng, options, legacyOptions) {
if (typeof options === "number") {
options = Util.extend({}, legacyOptions, { radius: options });
}
Util.setOptions(this, options);
this._latlng = toLatLng(latlng);
if (isNaN(this.options.radius)) {
throw new Error("Circle radius cannot be NaN");
}
this._mRadius = this.options.radius;
},
setRadius: function (radius) {
this._mRadius = radius;
return this.redraw();
},
getRadius: function () {
return this.mRadius;
},
getBounds: function () {
var half = [this._radius, this._radiusY || this._radius];
return new LatLngBounds(
this._map.layerPointToLatLng(this._point.subtract(half)),
this._map.layerPointToLatLng(this._point.add(half))
);
},
setStyle: Path.prototype.setStyle,
_project: function () {
var lng = this._latlng.lng,
lat = this._latlng.lat,
map = this._map,
crs = map.options.crs;
if (crs.distance === Earth.distance) {
var d = Math.PI / 180,
latR = this._mRadius / Earth.R / d,
top = map.project([lat + latR, lng]),
bottom = map.project([lat - latR, lng]),
p = top.add(bottom).divideBy(2),
lat2 = map.unproject(p).lat,
lngR =
Math.acos(
(Math.cos(latR * d) - Math.sin(lat * d) * Math.sin(lat2 * d)) /
(Math.cos(lat * d) * Math.cos(lat2 * d))
) / d;
if (isNaN(lngR) || lngR === 0) {
lngR = latR / Math.cos((Math.PI / 180) * lat); // Fallback for edge case, #2425
}
this._point = p.subtract(map.getPixelOrigin());
this._radius = isNaN(lngR) ? 0 : p.x - map.project([lat2, lng - lngR]).x;
this._radiusY = p.y - top.y;
} else {
var latlng2 = crs.unproject(
crs.project(this._latlng).subtract([this._mRadius, 0])
);
this._point = map.latLngToLayerPoint(this._latlng);
this._radius = this._point.x - map.latLngToLayerPoint(latlng2).x;
}
this._updateBounds();
},
});
export function circle(latlng, options, legacyOptions) {
return new Circle(latlng, options, legacyOptions);
}
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
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
编辑 (opens new window)
上次更新: 2025/04/15, 08:40:23