Leaflet
Use this skill for practical 2D web map work with Leaflet.
Leaflet is best for lightweight interactive maps that need simple setup, raster or XYZ tile layers, GeoJSON overlays, and straightforward custom controls. It is usually the right choice when the user wants a dependable browser map without the heavier rendering model of WebGL-first stacks.
Workflow
- 1. Confirm the runtime first:
- plain HTML
- Vite
- React
- Next.js or another SPA framework
- 2. Confirm the actual map job:
- base map only
- markers and popups
- GeoJSON overlay
- choropleth or feature styling
- draw/edit interaction
- bug fixing or migration
- 3. Start from the smallest visible map before adding controls, overlays, clustering, or plugins.
- Check container size and CSS before debugging data or tile logic.
- Keep data CRS assumptions explicit; Leaflet usually expects WGS84 longitude/latitude input for GeoJSON-style work.
Implementation Guardrails
- - Ensure the map container has a real height before initializing the map.
- Use
L.map(...) once per container and clean it up in SPA teardown paths. - Fit bounds from real data instead of hardcoding zoom/center when the dataset is dynamic.
- Prefer
L.geoJSON(...) for lightweight vector overlays and feature-level styling. - Keep tile URLs configurable and attribute them correctly.
- Use clustering or canvas-based rendering when marker counts grow large.
- Treat plugins as optional dependencies; verify maintenance status and compatibility before adopting them.
Common Failure Checks
- - Blank map: verify container height, CSS import, tile URL, and network access.
- Tiles not loading: verify URL template, attribution requirements, CORS behavior, and rate limits.
- Features in the wrong place: verify coordinate order and whether the source data is really WGS84 lon/lat.
- Map rendering oddly after resize or tab switch: call
map.invalidateSize() after the container becomes visible. - Memory leaks in React or SPA routes: remove the map instance and detach listeners on teardown.
- Slow interaction with many points: cluster points, simplify GeoJSON, or switch from DOM-heavy markers to canvas-friendly rendering.
Practical Patterns
Minimal map
- - Create the map only after the container exists.
- Add one tile layer with attribution.
- Set a sane initial center/zoom or fit to bounds from data.
GeoJSON overlay
- - Use
L.geoJSON with explicit style and onEachFeature. - Keep popup content deterministic and escape or sanitize user-provided text.
- Fit to the resulting layer bounds when the overlay is the main subject.
Interactive app integration
- - Store the map instance outside repeated render loops.
- Update layers incrementally instead of recreating the entire map on every state change.
- Separate base layer setup from data overlay updates.
Drawing and editing
- - Use a maintained drawing plugin only when the user explicitly needs editing.
- Confirm output format, CRS assumptions, and persistence flow before wiring edit tools.
Task Boundaries
- - Use this skill for Leaflet-specific implementation, debugging, and integration patterns.
- For CRS and reprojection decisions, use
project or wgs84. - For China-specific CGCS2000 reasoning, use
cgcs2000. - For file-based GIS conversion or batch reprojection, use
qgis. - For Mapbox GL JS or vector-tile style expressions, use
mapbox. - For CesiumJS globe rendering, use
cesium.
What To Return
- - A minimal working Leaflet setup or a targeted fix.
- The likely root cause when debugging a broken map.
- Clear notes on tile source assumptions, coordinate order, and CRS constraints.
- Any required cleanup or lifecycle handling for the framework in use.
- A smaller reproducible example first if the original integration is too noisy.
OpenClaw + ClawHub Notes
- - Keep examples generic, portable, and safe to paste into local projects.
- Do not hardcode private tile servers, private tokens, or machine-specific paths.
- Prefer maintained public dependencies and standards-based examples.
- For clawhub.ai publication, keep examples reproducible and version/changelog updates semver-driven.
Reference Docs In This Skill
- - Read
{baseDir}/references/patterns.md when generating or fixing Leaflet code for vanilla HTML, Vite, React, or GeoJSON-heavy pages.
Leaflet
使用此技能进行基于Leaflet的实用二维Web地图工作。
Leaflet最适合需要简单设置、栅格或XYZ切片图层、GeoJSON叠加层以及直接自定义控件的轻量级交互式地图。当用户需要一个可靠的浏览器地图,而不需要WebGL优先堆栈的较重渲染模型时,它通常是正确的选择。
工作流程
- 1. 首先确认运行时环境:
- 纯HTML
- Vite
- React
- Next.js或其他SPA框架
- 2. 确认实际地图任务:
- 仅底图
- 标记和弹出窗口
- GeoJSON叠加层
- 分级统计图或要素样式
- 绘制/编辑交互
- 错误修复或迁移
- 3. 在添加控件、叠加层、聚类或插件之前,从最小的可见地图开始。
- 在调试数据或切片逻辑之前,检查容器大小和CSS。
- 保持数据CRS假设明确;Leaflet通常期望GeoJSON样式的工作使用WGS84经度/纬度输入。
实施准则
- - 在初始化地图之前,确保地图容器具有实际高度。
- 每个容器仅使用一次L.map(...),并在SPA拆卸路径中进行清理。
- 当数据集是动态时,根据实际数据拟合边界,而不是硬编码缩放/中心点。
- 优先使用L.geoJSON(...)进行轻量级矢量叠加层和要素级样式设置。
- 保持切片URL可配置并正确标注属性。
- 当标记数量增多时,使用聚类或基于画布的渲染。
- 将插件视为可选依赖项;在采用之前验证维护状态和兼容性。
常见故障检查
- - 空白地图:检查容器高度、CSS导入、切片URL和网络访问。
- 切片未加载:检查URL模板、属性要求、CORS行为和速率限制。
- 要素位置错误:检查坐标顺序以及源数据是否确实是WGS84经度/纬度。
- 调整大小或切换标签页后地图渲染异常:在容器变为可见后调用map.invalidateSize()。
- React或SPA路由中的内存泄漏:在拆卸时移除地图实例并分离监听器。
- 大量点的交互缓慢:对点进行聚类、简化GeoJSON,或从DOM密集型标记切换到画布友好型渲染。
实用模式
最小地图
- - 仅在容器存在后创建地图。
- 添加一个带有属性的切片图层。
- 设置合理的初始中心点/缩放级别,或根据数据拟合边界。
GeoJSON叠加层
- - 使用带有显式样式和onEachFeature的L.geoJSON。
- 保持弹出内容确定性,并转义或清理用户提供的文本。
- 当叠加层是主要内容时,拟合到结果图层边界。
交互式应用集成
- - 将地图实例存储在重复渲染循环之外。
- 增量更新图层,而不是在每次状态更改时重新创建整个地图。
- 将底图设置与数据叠加层更新分开。
绘制和编辑
- - 仅在用户明确需要编辑时使用维护良好的绘制插件。
- 在连接编辑工具之前,确认输出格式、CRS假设和持久化流程。
任务边界
- - 使用此技能进行Leaflet特定的实现、调试和集成模式。
- 对于CRS和重投影决策,使用project或wgs84。
- 对于中国特定的CGCS2000推理,使用cgcs2000。
- 对于基于文件的GIS转换或批量重投影,使用qgis。
- 对于Mapbox GL JS或矢量切片样式表达式,使用mapbox。
- 对于CesiumJS地球渲染,使用cesium。
返回内容
- - 一个最小可用的Leaflet设置或针对性的修复。
- 调试损坏地图时可能的根本原因。
- 关于切片源假设、坐标顺序和CRS约束的清晰说明。
- 所使用框架所需的任何清理或生命周期处理。
- 如果原始集成过于复杂,首先提供一个更小的可重现示例。
OpenClaw + ClawHub 说明
- - 保持示例通用、可移植且安全,可直接粘贴到本地项目中。
- 不要硬编码私有切片服务器、私有令牌或特定机器的路径。
- 优先使用维护良好的公共依赖项和基于标准的示例。
- 对于clawhub.ai发布,保持示例可重现,版本/变更日志更新遵循语义化版本控制。
此技能中的参考文档
- - 在为纯HTML、Vite、React或GeoJSON密集型页面生成或修复Leaflet代码时,阅读{baseDir}/references/patterns.md。