# @bluehalo/ngx-leaflet-draw 中文文档

规范地址：https://npmcn.com/package/%40bluehalo/ngx-leaflet-draw/

版本：22\.0\.0
npm 元数据更新时间：2026-06-16T14:12:53.152Z
中文译文更新时间：2026-11-08T08:31:16.169Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40bluehalo/ngx-leaflet-draw
原文对照：https://npmcn.com/package/%40bluehalo/ngx-leaflet-draw/?lang=original
项目主页：https://github.com/bluehalo/ngx-leaflet-draw#readme
源码仓库：https://github.com/bluehalo/ngx-leaflet-draw

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：40.4 kB，7 个文件
- 模块格式：ES Module
- TypeScript 类型：包内自带
- 运行依赖：1 个
- peer 依赖：@angular/common 22，@angular/core 22，@bluehalo/ngx-leaflet 22，@types/leaflet 1，@types/leaflet-draw 1，leaflet 1，leaflet-draw 1
- 首次发布：2024-06-03T19:48:30.268Z
- 最新版本发布：2026-06-16T14:12:52.996Z
- 近 30 天下载量：20470（2026-10-09 至 2026-11-07，npm 官方下载统计）
- 已发布版本数：12（近 12 个月 6 个）

## 中文简介

Leaflet Draw 插件的 Angular.io 组件

## 安装

```sh
npm install @bluehalo/ngx-leaflet-draw
```

也可以使用：`pnpm add @bluehalo/ngx-leaflet-draw`、`yarn add @bluehalo/ngx-leaflet-draw`、`bun add @bluehalo/ngx-leaflet-draw`

## README

# @bluehalo/ngx-leaflet-draw(英语:

[![NPM version](https://img.shields.io/npm/v/%40bluehalo%2Fngx-leaflet-draw)](https://www.npmjs.com/package/@bluehalo/ngx-leaflet-draw) [![Build Status](https://github.com/bluehalo/ngx-leaflet-draw/actions/workflows/ci.yml/badge.svg)](https://github.com/bluehalo/ngx-leaflet-draw/actions/workflows/ci.yml) [![Code Coverage](https://codecov.io/gh/bluehalo/ngx-leaflet-draw/graph/badge.svg)](https://codecov.io/gh/bluehalo/ngx-leaflet-draw)

> Leaflet Draw 扩展到 Angular.io 的 @ bluehalo/ngx-leaflet 包 提供Leaflet Draw集成于Angular.io项目. 与 Leaflet v1.x 和 Leaflet Draw 1.x 兼容

## 目录

-   [安装](https://npmcn.com/package/%40bluehalo/ngx-leaflet-draw/#readme-install)
-   [使用量](https://npmcn.com/package/%40bluehalo/ngx-leaflet-draw/#readme-usage)
-   [API 密码](https://npmcn.com/package/%40bluehalo/ngx-leaflet-draw/docs/API.md)
-   [烹饪本](https://npmcn.com/package/%40bluehalo/ngx-leaflet-draw/docs/cookbook.md)
-   [贡献](https://npmcn.com/package/%40bluehalo/ngx-leaflet-draw/#readme-contribute)
-   [许可证](https://npmcn.com/package/%40bluehalo/ngx-leaflet-draw/#readme-license)
-   [贷项](https://npmcn.com/package/%40bluehalo/ngx-leaflet-draw/#readme-credits)

## 安装

通过 npm(或纱线) 安装软件包及其对等依赖性 :

```shell
npm install leaflet
npm install leaflet-draw@1.0.2
npm install @bluehalo/ngx-leaflet
npm install @bluehalo/ngx-leaflet-draw
```

注:我见过一些与 `leaflet-draw@1.0.3` 财务报告和审定财务报表 `1.0.4`。 。 。 . 具体地说,标签锚和绘图矩似乎并不正确. 如果你有问题, `leaflet-draw@1.0.2` 似乎是没有这些问题的最新发行.

您还需要通过 npm 安装传单打字:

```shell
npm install --save-dev @types/leaflet
npm install --save-dev @types/leaflet-draw
```

如果您想要运行演示, 克隆寄存器, 执行 `npm install`, (中文(简体) ), `npm run demo` 接着去 [http://localhost:4200 (英语)](http://localhost:4200/)。 。 。 .

## 使用量

这些步骤一般是:

-   安装和配置 [@bluehalo/ngx-leaflet(英语:](https://github.com/bluehalo/ngx-leaflet)
-   安装此库并安装 Leaflet- draw 打字(见上文) .
-   导入叶片和叶片绘制样式表
-   导入 `LeafletModule` 财务报告和审定财务报表 `LeafletDraw` 输入您的角工程
-   创建和配置地图(见下文和/或演示文件)

### 导入叶片样式表

要使用传单,请将传单样式表加载到您的应用程序中. 如果你通过npm安装, 你需要加载两者 `./node_modules/leaflet/dist/leaflet.css` 财务报告和审定财务报表 `./node_modules/leaflet-draw/dist/leaflet.draw.css`。 。 。 . 如何包含样式表将取决于您的特定设置 。 例子请参见: [@bluehalo/ngx-leaflet(英语:](https://github.com/bluehalo/ngx-leaflet) 读取.

如果您正在使用 Angular CLI, 您需要将 CSS 文件添加到包含在其中的样式数组中 `angular.json`数字 :

```json
{
    ...
    "styles": [
        "styles.css",
        "./node_modules/leaflet/dist/leaflet.css",
        "./node_modules/leaflet-draw/dist/leaflet.draw.css"
    ],
    ...
}
```

### 导入叶片模块和叶片Draw模块

在可以使用您 Angular.io app中的Leaflet 组件之前,您需要在应用程序中导入. 根据您是否使用独立模式, 您将导入您的模块和/ 或组件 .

```typescript
import { LeafletDirective, LeafletLayerDirective } from '@bluehalo/ngx-leaflet';
import { LeafletDrawDirective } from '@bluehalo/ngx-leaflet-draw';

@Component({
    imports: [
        //...
        LeafletDirective,
        LeafletLayerDirective,
        LeafletDrawDirective
    ]
})
```

### 创建和配置带有绘图控件的地图

要创建地图,请使用 `leaflet` 属性指令。 此指令必须先出. 您必须通过 `leafletOptions` 或通过约束 `leafletZoom`, (中文(简体) ), `leafletCenter`,以及 `leafletLayers`。 。 。 . 最后,添加: `leafletDraw` 属性指令,用于添加传单绘图控制并配置其中 `leafletDrawOptions`。 。 。 .

```angular181html
<div leaflet style="height: 400px;"
     leafletDraw
     [leafletOptions]="options"
     [leafletDrawOptions]="drawOptions"
     (leafletDrawCreated)="onDrawCreated($event)">

    <div [leafletLayer]="drawnItems"></div>

</div>
```

要启用编辑, 您需要添加一个 `featureGroup` 到地图上,然后将特性组与 `drawOptions`。 。 。 . 此外,您还需要在功能组合中添加层,因为此功能将不再自动发生. 这两种变化都是新的 `@bluehalo/ngx-leaflet-draw@6`,用于匹配默认的Leaflet Draw行为.

```typescript
import { DrawEvents, FeatureGroup, featureGroup, icon } from 'leaflet';

drawnItems: FeatureGroup = featureGroup();

drawOptions = {
    draw: {
        marker: {
            icon: icon({
                iconSize: [ 25, 41 ],
                iconAnchor: [ 13, 41 ],
                iconUrl: 'assets/leaflet/marker-icon.png',
                iconRetinaUrl: 'assets/leaflet/marker-icon-2x.png',
                shadowUrl: 'assets/leaflet/marker-shadow.png'
            })
        }
    },
    edit: {
        featureGroup: this.drawnItems
    }
};

public onDrawCreated(e: any) {
    this.drawnItems.addLayer((e as DrawEvents.Created).layer);
}
```

### 关于标记的说明

如果您正在使用 Angular CLI 或 Webpack 来包装您的工程, 您需要配置标记图标, 如 `drawOptions` 上文的例子. 这个问题与Leaflet如何处理图标图像加载有关. 有关如何设置的更详细情况,请参考 [标记设置](https://github.com/bluehalo/ngx-leaflet/blob/master/docs/cookbook.md#marker-setup) 在 @bluehalo/ngx-leaflet 烹饪簿中提供指南.

## API 密码

完整的 API 文档在 [文件/API.md](https://npmcn.com/package/%40bluehalo/ngx-leaflet-draw/docs/API.md)它涵盖:

-   指令投入: `leafletDraw`, (中文(简体) ), `leafletDrawOptions`, (中文(简体) ), `leafletDrawLocal`
-   绘制事件——全部 16 `L.Draw.Event` 输出绑定
-   通过 `leafletDrawReady` 或者说 `LeafletDrawDirective` 注射

## 烹饪本

共同模式和实例 [文件/教科书.md](https://npmcn.com/package/%40bluehalo/ngx-leaflet-draw/docs/cookbook.md),包括:

-   [显示并隐藏绘图控制](https://npmcn.com/package/%40bluehalo/ngx-leaflet-draw/docs/cookbook.md#showing-and-hiding-the-draw-control)

## 贡献

公关接受。 请对功能分支投稿 并打开一个吸引请求反对 `master`。 。 。 .

## 许可证

见 [自由主义](https://npmcn.com/package/%40bluehalo/ngx-leaflet-draw/LICENSE) 详细情况.

## 贷项

**[传单](http://leafletjs.com/)** 是一个很棒的地图包. **[叶子. draw](https://github.com/Leaflet/Leaflet.draw)** 使得地图上的画形变得非常容易.

## 常见问题

### @bluehalo/ngx-leaflet-draw 是什么？

Leaflet Draw 插件的 Angular\.io 组件

### 如何安装 @bluehalo/ngx-leaflet-draw？

在项目目录运行 npm install @bluehalo/ngx-leaflet-draw，或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖：@angular/common（22）、@angular/core（22）、@bluehalo/ngx-leaflet（22）、@types/leaflet（1）、@types/leaflet-draw（1）、leaflet（1）、leaflet-draw（1）。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖，Yarn 需要手动安装。

### @bluehalo/ngx-leaflet-draw 支持 TypeScript 吗？

支持。包内自带 TypeScript 类型声明（package\.json 声明了 types 入口），安装后即可获得类型提示，无需再安装 @types 包。

### @bluehalo/ngx-leaflet-draw 支持 ESM 还是 CommonJS？

以 ES Module 形式发布，推荐使用 import 引入。在 CommonJS 项目中，Node\.js 20\.19\+ / 22\.12\+ 可以直接 require\(\) 不含顶层 await 的 ES Module，更早的版本需要改用动态 import\(\)。

### @bluehalo/ngx-leaflet-draw 可以商用吗？

@bluehalo/ngx-leaflet-draw 使用 MIT 许可。宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。以上为概括，不构成法律意见，请以许可原文为准。

### @bluehalo/ngx-leaflet-draw 的下载量有多少？

根据 npm 官方下载统计，@bluehalo/ngx-leaflet-draw 在 2026/10/09 – 2026/11/07 这 30 天内被下载 20,470 次，日均约 682 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @bluehalo/ngx-leaflet-draw 还在维护吗？

最新版本 22\.0\.0 发布于 2026/06/16（4 个月前）；至今共发布 12 个版本，其中过去 12 个月发布了 6 个；首次发布于 2024/06/04。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

