Atlaslog
Atlaslog / Leaflet / API reference / PolyUtil
Leaflet · API reference

PolyUtil

Quick answer

L.PolyUtil clips polygon rings to a pixel rectangle and computes area weighted or vertex averaged centers, mirroring the math Leaflet's renderers perform. Use it when writing custom canvas drawing or when a label needs a sensible anchor inside a polygon.

Last verified 2026-11-03 · Leaflet 1.9.4 · Cite this page
L.PolyUtil. clipPolygon ( points , bounds , round ?) → Point[]
Key points
Example
var size = map.getSize();
var box = new L.Bounds(L.point(0, 0), L.point(size.x, size.y));

var ring = polygon.getLatLngs()[0].map(function (latlng) {
  return map.latLngToLayerPoint(latlng);
});

var clipped = L.PolyUtil.clipPolygon(ring, box, true);

ctx.beginPath();
clipped.forEach(function (p, i) {
  if (i === 0) { ctx.moveTo(p.x, p.y); }
  else { ctx.lineTo(p.x, p.y); }
});
ctx.closePath();
ctx.fill();
var ring = polygon.getLatLngs()[0];

var center = L.PolyUtil.polygonCenter(ring, map.options.crs);
var average = L.PolyUtil.centroid(ring);

L.marker(center).bindTooltip('面积重心').addTo(map);
L.circleMarker(average, { radius: 4 }).bindTooltip('顶点平均').addTo(map);
Notes
Runnable examplesChinese originalAll API references
English edition written for Atlaslog. Code is taken from the original Chinese page and runs unchanged.
Cite this page

Atlaslog (2026). PolyUtil. https://www.jingweizhi.com/en/leaflet/api/poly-util.html (accessed 2026-11-03).