Atlaslog
Atlaslog / Leaflet / API reference / LineUtil
Leaflet · API reference

LineUtil

Quick answer

L.LineUtil offers polyline geometry helpers for simplification, viewport clipping, point to segment distance and length weighted centers, all computed on pixel point arrays. Every routine expects L.Point instances in layer pixel space, never raw latitude and longitude pairs. Convert coordinates with latLngToLayerPoint before calling any of them.

Last verified 2026-11-03 · Leaflet 1.9.4 · Cite this page
L.LineUtil. simplify ( points , tolerance ) → Point[]
Key points
Example
var latlngs = polyline.getLatLngs();

var pts = latlngs.map(function (latlng) {
  return map.latLngToLayerPoint(latlng);
});

/* 容差 1.5 像素:缩放后肉眼几乎看不出差别 */
var thin = L.LineUtil.simplify(pts, 1.5);

var back = thin.map(function (p) {
  return map.layerPointToLatLng(p);
});

console.log(pts.length, '→', thin.length);
polyline.setLatLngs(back);
var size = map.getSize();
var box = new L.Bounds(L.point(0, 0), L.point(size.x, size.y));

var a = map.latLngToLayerPoint(latlngA);
var b = map.latLngToLayerPoint(latlngB);

var clipped = L.LineUtil.clipSegment(a, b, box, false, true);

if (clipped) {
  drawLine(clipped[0], clipped[1]);
}
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). LineUtil. https://www.jingweizhi.com/en/leaflet/api/line-util.html (accessed 2026-11-03).