【问题标题】:Use Leaflet Draw plugin in Angular 7 TypeScript Component's Toolbar在 Angular 7 TypeScript 组件的工具栏中使用 Leaflet Draw 插件
【发布时间】:2019-08-03 08:47:23
【问题描述】:

我正在尝试在我的 Angular 7 项目中使用 Leaflet.Illustrate。我试图在 HTML 中加载它,但是,即使它确实加载了,也不清楚如何调用

new L.Illustrate.Control({
    edit: { featureGroup: drawnItems }
}).addTo(map);

将其添加到现有调用以构建我拥有的工具栏:

addDraw() {
    if (this.map !== undefined) {
          const leaflet = this.map.leafletMap();
          leaflet.setZoom(3);

          const drawnItems = new L.FeatureGroup();
          leaflet.addLayer(drawnItems);

          const drawControl = new L.Control.Draw({
            position: 'bottomright',
            draw: {
              polygon: {
                allowIntersection: false,
                showArea: true
              }
            },
            edit: {
              featureGroup: drawnItems
            }
          });

          leaflet.addControl(drawControl);

          leaflet.on(L.Draw.Event.CREATED, function (event: any) {
            drawnItems.addLayer(event.layer);
          });
        }
}

我一直在寻找@types/leaflet-illustrate,但没有运气,我什至打算尝试编写一个 index.d.ts 来涵盖 JavaScript 库。有没有其他人运气好,或者找到如何编写 index.d.ts 的好方法?

【问题讨论】:

    标签: angular typescript leaflet typescript-typings leaflet.draw


    【解决方案1】:

    插件似乎不支持1.0.0以后的leaflet版本和0.2.x以后的leaflet-draw版本

    因此,为了能够使用它,您需要使用旧版本的传单和传单绘制,更具体地说是传单 0.7.x

    安装leaflet 0.7.2leaflet-draw 0.2.4leaflet-illustrate 0.0.3

    在angular.json中导入css文件如下:

    "styles": [
         "../node_modules/leaflet/dist/leaflet.css",
         "../node_modules/leaflet-draw/dist/leaflet.draw.css",
         "../node_modules/leaflet-illustrate/dist/Leaflet.Illustrate.css",
         "styles.css"
    ],
    ...
    

    在 .ts 中放置以下代码:

    ngOnInit() {
        var map = L.map("map").setView([41.7896, -87.5996], 15);
        L.tileLayer("http://otile1.mqcdn.com/tiles/1.0.0/sat/{z}/{x}/{y}.jpg", {
          attribution:
            'Tiles Courtesy of <a href="http://www.mapquest.com/">MapQuest</a> &mdash; Portions Courtesy NASA/JPL-Caltech and U.S. Depart. of Agriculture, Farm Service Agency'
        }).addTo(map);
    
        map.on("click", function(evt) {
          console.log(evt);
        });
    
        var drawnItems = new L.FeatureGroup();
        map.addLayer(drawnItems);
    
        var illustrateControl = new L.Illustrate.Control({
          edit: {
            featureGroup: drawnItems
          }
        });
        map.addControl(illustrateControl);
    
        drawnItems.addLayer(
          new L.Illustrate.Pointer(L.latLng(41.7868010411136, -87.60601043701172), [
            new L.Point(0, 0),
            new L.Point(100, -100),
            new L.Point(400, -100)
          ])
        );
        drawnItems.addLayer(
          new L.Illustrate.Pointer(
            L.latLng(41.80219068741082, -87.60648250579834),
            [new L.Point(0, 0), new L.Point(100, -100), new L.Point(400, -100)]
          )
        );
    
        map.on("draw:created", function(evt) {
          var type = evt.layerType,
            layer = evt.layer;
    
          drawnItems.addLayer(layer);
        });
    }
    

    Demo

    【讨论】:

    • 感谢您的回复。我认为你的演示很棒。问题是,我无法恢复到早期版本。我目前正在努力将leaflet-illustrate 的功能添加到我的本地leaflet draw 副本中。我希望能够为当前的传单绘制创建一个插件,然后从那里开始。我真的只需要文字。到目前为止,它似乎还不错。关于如何为 Leaflet-draw 1.0.4 的 JS 做一个插件的任何建议,所以我不必有一份不能有 npm 更新的传单绘图副本,我会失去我所有的代码?非常感谢您的帮助。
    • 不要使用当时已弃用的库。检查this。尚未测试,但可能有效
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-14
    相关资源
    最近更新 更多