@bluehalo/ngx-leaflet-draw

中文文档
22.0.0发布于 2026/06/16MITTypeScriptES Module近 30 天下载 2万

Angular.io components for the Leaflet Draw plugin

原始使用文档
文档目录12 节
  1. Table of Contents
  2. Install
  3. Usage
  4. Import the Leaflet Stylesheet
  5. Import LeafletModule and LeafletDrawModule
  6. Create and Configure a Map with the Draw Controls
  7. A Note About Markers
  8. API
  9. Cookbook
  10. Contribute
  11. License
  12. Credits

@bluehalo/ngx-leaflet-draw

NPM version Build Status Code Coverage

Leaflet Draw extension to the @bluehalo/ngx-leaflet package for Angular.io Provides Leaflet Draw integration into Angular.io projects. Compatible with Leaflet v1.x and Leaflet Draw 1.x

Table of Contents

Install

Install the package and its peer dependencies via npm (or yarn):

npm install leaflet
npm install [email protected]
npm install @bluehalo/ngx-leaflet
npm install @bluehalo/ngx-leaflet-draw

NOTE: I've seen some issues with [email protected] and 1.0.4. Specifically, label anchors and drawing rectangles doesn't seem to work correctly. If you have issues, [email protected] seems to be the latest release without those issues.

You will need to also install the leaflet typings via npm:

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

If you want to run the demo, clone the repository, perform an npm install, npm run demo and then go to http://localhost:4200.

Usage

Generally, the steps are:

  • Install and configure @bluehalo/ngx-leaflet
  • Install this library and the Leaflet-draw typings (see above).
  • Import the Leaflet and Leaflet-draw stylesheet
  • Import LeafletModule and LeafletDraw into your Angular project
  • Create and configure a map (see docs below and/or demo)

Import the Leaflet Stylesheet

For leaflet to work, you need to have the leaflet stylesheets loaded into your application. If you've installed via npm, you will need to load both ./node_modules/leaflet/dist/leaflet.css and ./node_modules/leaflet-draw/dist/leaflet.draw.css. How you include the stylesheet will depend on your specific setup. For examples, refer to the @bluehalo/ngx-leaflet README.

If you are using Angular CLI, you will need to add the CSS files to the styles array contained in angular.json:

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

Import LeafletModule and LeafletDrawModule

Before you can use the Leaflet components in your Angular.io app, you'll need to import it in your application. Depending on if you're using standalone mode or not, you will import it into your modules and/or components.

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

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

Create and Configure a Map with the Draw Controls

To create a map, use the leaflet attribute directive. This directive must appear first. You must specify an initial zoom/center and set of layers either via leafletOptions or by binding to leafletZoom, leafletCenter, and leafletLayers. Finally, add the leafletDraw attribute directive to add the leaflet draw control and configure it with leafletDrawOptions.

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

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

</div>

To enable editing, you need to add a featureGroup to the map and pass the feature group in with the drawOptions. In addition, you will need to add layers to the feature group yourself, as this will no longer happen automatically. Both of these changes are new in @bluehalo/ngx-leaflet-draw@6, and were made to match default Leaflet Draw behavior.

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);
}

A Note About Markers

If you are using Angular CLI or Webpack to package your project, you will need to configure the marker icon as shown in the drawOptions example above. The issue has to do with how Leaflet handles icon image loading. For more details on how to set this up, reference the Marker Setup guide in the @bluehalo/ngx-leaflet cookbook.

API

Full API documentation is in docs/API.md. It covers:

  • Directive inputs: leafletDraw, leafletDrawOptions, leafletDrawLocal
  • Draw events — all 16 L.Draw.Event output bindings
  • Getting a reference to the draw control via leafletDrawReady or LeafletDrawDirective injection

Cookbook

Common patterns and examples are in docs/cookbook.md, including:

Contribute

PRs accepted. Please make contributions on feature branches and open a pull request against master.

License

See LICENSE for details.

Credits

Leaflet Is an awesome mapping package. Leaflet.draw Makes drawing shapes on maps super easy.

版本与发布记录

@bluehalo/ngx-leaflet-draw 共发布 12 个版本,首次发布于 2024/06/04;过去 12 个月发布了 6 个版本。

版本发布日期距今
22.0.0latest2026/06/164 个月前
21.1.32026/05/066 个月前
21.1.22026/04/017 个月前
21.1.12026/03/317 个月前
21.1.02026/03/317 个月前
21.0.02026/03/317 个月前

关于 @bluehalo/ngx-leaflet-draw 的常见问题

@bluehalo/ngx-leaflet-draw 是什么?

Angular.io components for the Leaflet Draw plugin

如何安装 @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 和提交记录判断。