Jinuss's blog Jinuss's blog
首页
  • 源码合集

    • Leaflet源码分析
    • Openlayers源码合集
    • vue3源码
  • HTML
  • CSS
  • 技术文档
  • GitHub技巧
  • 学习
  • 实用技巧
关于
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

东流

Web、WebGIS技术博客
首页
  • 源码合集

    • Leaflet源码分析
    • Openlayers源码合集
    • vue3源码
  • HTML
  • CSS
  • 技术文档
  • GitHub技巧
  • 学习
  • 实用技巧
关于
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • 框架

    • Leaflet基础介绍
      • 概览
        • 源码介绍
      • Leaflet.js 模块化架构介绍
        • 1. `control` 模块
        • 2. `core` 模块
        • 3. `dom` 模块
        • 4. `geometry` 模块
        • 5. `geo` 模块
        • 6. `layer` 模块
        • 7. `map` 模块
        • 模块化开发的意义
  • core模块

  • dom模块

  • control

  • geometry

  • geo

  • layer

  • Map

  • 《Leaflet源码》笔记
  • 框架
东流
2025-02-27
目录

Leaflet基础介绍

# 概览

Leaflet.js 是一个开源的 JavaScript 库,用于创建交互式地图。它的模块化架构设计使得代码结构清晰、易于扩展和维护。以下是 Leaflet.js 模块化架构的核心模块分类:

  1. control: 控件相关的模块,比如地图缩放控件、定位按钮、图层切换器等。Leaflet 默认有一些控件,用户也可以自定义。

  2. core: 核心模块,处理地图的基本功能,比如视图管理、事件系统、坐标转换等。这部分是 Leaflet 运行的基础。

  3. dom: 处理 DOM 操作,比如元素创建、样式管理、事件监听。可能在渲染地图元素时用到。

  4. geometry: 几何图形相关,如点、线、多边形的计算和操作,可能涉及坐标变换和几何算法。

  5. geo: 地理编码和解码,比如将地址转换为坐标(逆地理编码)或坐标转换为地址(正地理编码),可能集成第三方服务。

  6. layer: 图层管理,如矢量图层、瓦片图层、标记(Marker)、圆圈等。用户通常会在这里添加各种地图元素。

  7. map: 地图实例的核心类,负责初始化地图、管理图层、处理用户交互等。

# 源码介绍

在 Leaflet.js的1.9.4版本中,模块化架构的核心模块分类是由 Leaflet.js 文件导出的。以下是 Leaflet.js 文件的内容:

import { version } from "../package.json";
export { version };

// control
export * from "./control/index";

// core
export * from "./core/index";

// dom
export * from "./dom/index";

// geometry
export * from "./geometry/index";

// geo
export * from "./geo/index";

// layer
export * from "./layer/index";

// map
export * from "./map/index";
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

# Leaflet.js 模块化架构介绍

Leaflet.js 模块化架构中的核心模块分类,每个模块负责特定功能领域的实现。以下是各模块的简要说明:

# 1. control 模块

  • 作用:管理地图上的用户交互控件(如缩放条、定位按钮、图层切换器等)。

  • 核心功能:

    • 提供默认控件(ZoomControl, AttributionControl)。

    • 支持自定义控件开发(通过 L.Control.extend)。

    • 控件位置管理(如 bottomright, topleft)。

  • 示例:

    // 添加缩放控件
    L.control.zoom().addTo(map);
    
    // 自定义控件
    class MyCustomControl extends L.Control {
      onAdd(map) {
        this._container = L.DomUtil.create("div", "my-control");
        return this._container;
      }
    }
    L.control.myCustom = MyCustomControl;
    L.control.myCustom.addTo(map);
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
序号 文章
1 Control基类 (opens new window)
2 Control.Zoom缩放 (opens new window)
3 Control.Scale比例尺 (opens new window)
4 Control.Attribution属性控件 (opens new window)
5 Control.Layers图层 (opens new window)

# 2. core 模块

  • 作用:Leaflet 的核心运行逻辑,包含地图视图、事件系统、坐标系管理等底层机制。

  • 关键类/接口:

    • L.Evented:事件发布-订阅系统。

    • L.Handler:处理地图交互(如拖拽、缩放)的基类。

    • L.Map:地图实例的核心类(用户最常直接使用的对象)。

    • L.Projection:坐标系转换(如 WGS84 → Web Mercator)。

  • 示例:

    // 监听地图事件
    map.on("moveend", () => {
      console.log("地图位置变化:", map.getCenter());
    });
    
    // 自定义坐标系(简化示意)
    class MyProjection extends L.Projection {
      project(latlng) {
        /* ... */
      }
      unproject(x, y) {
        /* ... */
      }
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
序号 文章
6 Class基类 (opens new window)
7 Event事件类 (opens new window)
8 Util工具方法 (opens new window)
9 L.Browser浏览器检测 (opens new window)
10 Handle基类 (opens new window)

# 3. dom 模块

  • 作用:封装 DOM 操作和样式管理,与浏览器交互的底层工具。

  • 核心功能:

    • DOM 节点创建 (L.DomUtil.create)。

    • 样式操作 (L.DomUtil.addClass, L.DomUtil.getStyle)。

    • 事件代理和性能优化(如 requestAnimationFrame)。

  • 示例:

    // 创建带样式的 DOM 元素
    const div = L.DomUtil.create("div", "my-div", map.getPanes().overlayPane);
    div.style.fontSize = "12px";
    div.textContent = "Hello Leaflet!";
    
    1
    2
    3
    4
序号 文章
11 DomEvent事件 (opens new window)
12 DomEvent.Pointer (opens new window)
13 DomEvent.DoubleTap (opens new window)
14 Draggable拖拽 (opens new window)
15 PosAnimation位置动画 (opens new window)
16 DomUtil工具方法 (opens new window)

# 4. geometry 模块

  • 作用:处理几何图形的数学计算,如点、线、多边形的操作。

  • 关键类/接口:

    • L.Point:二维坐标点。

    • L.LatLng:地理坐标(经纬度)。

    • L.LineString、L.Polygon:矢量路径和多边形。

    • 几何操作(距离计算、交集检测等)。

  • 示例:

    // 计算两点距离
    const pointA = L.latLng(51.5, -0.09);
    const pointB = L.latLng(51.51, -0.1);
    const distance = pointA.distanceTo(pointB); // 单位:米
    
    1
    2
    3
    4
序号 文章
17 Point (opens new window)
18 Transformation对象 (opens new window)
19 LineUtil工具 (opens new window)
20 PolyUtil工具 (opens new window)

# 5. geo 模块

  • 作用:地理编码(Geocoding)和逆地理编码功能。

  • 核心功能:

    • 通过地址查询坐标(正向地理编码)。

    • 通过坐标查询地址(逆向地理编码)。

    • 集成第三方服务(如 OpenStreetMap Nominatim、高德地图)。

  • 示例:

    // 使用 OpenStreetMap 逆地理编码
    L.geocode("Paris, France").then((res) => {
      console.log(res[0].latlng); // 输出巴黎的经纬度
    });
    
    1
    2
    3
    4
序号 文章
21 LatLng (opens new window)
22 LatLngBounds (opens new window)
23 Projection.Mercator (opens new window)
24 Projection.LonLat (opens new window)
25 Projection.SphericalMercator (opens new window)
26 CRS对象 (opens new window)
27 CRS.Earth (opens new window)
28 CRS.Simple (opens new window)
29 常见坐标系3857、4326等 (opens new window)

# 6. layer 模块

  • 作用:管理地图图层的抽象层,支持叠加、移除和顺序控制。

  • 核心类:

    • L.LayerGroup:图层组(批量管理多个图层)。

    • L.TileLayer:瓦片图层(如 OpenStreetMap、卫星图)。

    • L.Marker、L.Circle:矢量标记。

    • L Popup:弹窗内容。

  • 示例:

    // 添加多个图层并分组控制
    const layer1 = L.tileLayer("https://{s}.tile.openstreetmap.org/{z}/{x}/{y}");
    const layer2 = L.tileLayer(
      "https://{s}.tile.mapbox.com/{z}/{x}/{y}/mapbox.streets-v11"
    );
    
    const baseLayers = {
      OpenStreetMap: layer1,
      "Mapbox Streets": layer2,
    };
    
    const map = L.map("map", { layers: [layer1] });
    L.control.layers(baseLayers).addTo(map);
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
序号 文章
30 Icon (opens new window)
31 Icon.default (opens new window)
32 DivIcon (opens new window)
33 Marker (opens new window)
34 Marker.Drag (opens new window)
35 Layer基类 (opens new window)
36 LayerGroup (opens new window)
37 FeatureGroup (opens new window)
38 DivOverlay (opens new window)
39 Popup (opens new window)
40 Tooltip (opens new window)
41 ImageOverlay (opens new window)
42 SVGOverlay (opens new window)
43 VideoOverlay (opens new window)
44 Path基类 (opens new window)
45 GeoJSON模块 (opens new window)
46 Renderer渲染器基类 (opens new window)
47 Canvas渲染器 (opens new window)
48 SVG渲染器 (opens new window)
49 Polyline (opens new window)
50 Polygon (opens new window)
51 Render.getRender (opens new window)
52 Rectangle (opens new window)
53 CircleMarker (opens new window)
54 Circle (opens new window)
55 GridLayer (opens new window)
56 TileLayer (opens new window)
57 TileLayer.WMS (opens new window)

# 7. map 模块

  • 作用:地图实例的顶层容器,整合所有模块功能。

  • 核心功能:

    • 初始化地图容器 (L.map('map', options))。

    • 管理图层栈 (map.addLayer(layer))。

    • 处理用户交互(如拖拽、缩放、点击)。

    • 坐标系和投影管理。

  • 示例:

    // 初始化地图
    const map = L.map("map", {
      center: [51.505, -0.09], // 纬度, 经度
      zoom: 13,
      layers: [L.tileLayer("https://{s}.tile.openstreetmap.org/{z}/{x}/{y}")],
    });
    
    // 添加标记
    L.marker([51.5, -0.09])
      .addTo(map)
      .bindPopup("<b>Hello!</b><br>I am a popup.");
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
序号 文章
58 Map类扩展方法之BoxZoom (opens new window)
59 Map类扩展方法之DoubleClickZoom (opens new window)
60 Map类扩展方法之Keyboard (opens new window)
61 Map类扩展方法之ScrollWheelZoom (opens new window)
62 Map类扩展方法之TapHold (opens new window)
63 Map类扩展方法之TouchZoom (opens new window)
64 Map类扩展方法之Drag (opens new window)

# 模块化开发的意义

Leaflet 将功能拆分为独立模块,主要优势:

  1. 按需加载:仅引入需要的模块(如移动端可省略 geo 模块)。

  2. 代码隔离:避免命名空间污染,提升维护性。

  3. 扩展性:开发者可替换或扩展特定模块(如自定义 tileLayer 的瓦片加载逻辑)。

  4. 性能优化:按需加载,减少初始加载时间。

编辑 (opens new window)
上次更新: 2025/05/28, 03:37:25
Leaflet自定义基类Class

Leaflet自定义基类Class→

最近更新
01
ECMAScript新特性汇总
07-14
02
GeoJSON
05-08
03
Circle
04-15
更多文章>
Theme by Vdoing | Copyright © 2024-2025 东流 | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式