地理坐标与投影
本文档涵盖 Leaflet 的坐标系统架构,包括地理坐标、像素坐标系统、投影系统(CRS) 以及在地理坐标和屏幕坐标之间的转换。关于 map view 管理和 zoom 级别的信息,请参阅 地图组件。关于 geometry utilities 和数学运算的详细信息,请参阅 工具与类系统。
Geographic 与 Pixel Coordinate Systems
Leaflet 使用两种基本的 coordinate space:表示地球位置的 geographic coordinates,以及表示屏幕上位置的 pixel coordinates。库为每个空间及其边界矩形提供专用的 class。
LatLng 与 Geographic Coordinates
LatLng Class 表示具有 latitude、longitude 和可选 altitude 值的地理点。它支持灵活的构造模式并提供地理计算。
| 构造模式 | 示例 | 描述 |
|---|---|---|
| 单独坐标 | new LatLng(50.5, 30.5) | Latitude、longitude、可选 altitude |
| 数组格式 | new LatLng([50.5, 30.5]) | [lat, lng] 或 [lat, lng, alt] |
| 对象格式 | new LatLng({lat: 50.5, lng: 30.5}) | 带有 lat/lng 或 lat/lon 的对象 |
该 class 提供基本的地理操作,包括使用 Haversine 公式的距离计算、longitude 边界的 coordinate wrapping,以及用于空间查询的 bounds 生成。
地理区域的 LatLngBounds
LatLngBounds 表示由西南和东北角点定义的矩形地理区域。它提供空间关系方法和边界计算。
Point 与 Pixel Coordinates
Point Class 处理 pixel coordinates 并提供全面的数学运算。与 geographic coordinates 不同,points 表示屏幕位置并支持向量数学。
Coordinate Reference Systems 架构
CRS (Coordinate Reference System) class 构成 Leaflet projection 系统的核心,通过涉及 projection 和 transformation 的两阶段过程在 geographic coordinates 和 pixel coordinates 之间转换。
CRS 基类与 Projection Pipeline
基类 CRS 定义 coordinate transformations 的标准接口。Projection pipeline 涉及两个关键阶段:将 geographic projection 到中间 coordinate system,然后 affine transformation 到 pixel coordinates。
CRS Class 提供这些核心转换方法:
| 方法 | 用途 | Pipeline 阶段 |
|---|---|---|
latLngToPoint(latlng, zoom) | Geographic 到 pixel coordinates | 完整 pipeline |
pointToLatLng(point, zoom) | Pixel 到 geographic coordinates | 反向 pipeline |
project(latlng) | Geographic 到 projected coordinates | 仅 projection |
unproject(point) | Projected 到 geographic coordinates | 反向 projection |
用于 Affine 操作的 Transformation Class
Transformation Class 使用系数 a、b、c、d 处理 affine transformations,将 points 从 (x, y) 转换为 (a*x + b, c*y + d)。CRS 实现使用它在特定 zoom 级别将 projected coordinates 转换为 pixel coordinates。
CRS 实现
Leaflet 包含多个 CRS 实现用于不同的 mapping 场景,每个都有特定的 projection 特性和用例。
基于 Earth 的 CRS 系统
Earth Class 作为全局 CRS 实现的基类,提供 longitude wrapping 和 Haversine 距离计算。
Web Mercator (EPSG:3857)
最常见的 web mapping CRS,EPSG3857 使用 Spherical Mercator projection,是大多数 tile provider 的默认设置。
非地理地图的 Simple CRS
CRS.Simple 提供 longitude/latitude 与 x/y coordinates 之间的直接 1:1 映射,适用于游戏地图或非地理图像。
Coordinate System 集成
Coordinate system 组件协同工作,在 geographic positions 和 screen rendering 之间提供无缝转换。系统自动处理 coordinate wrapping、bounds checking 和 zoom-level scaling。
这种架构使 Leaflet 能够支持从使用 Web Mercator projection 的标准 web 地图到需要自定义 coordinate systems 或非地理图像的专用应用等多样化的 mapping 场景。