标记未被删除:传单

时间:2015-09-16 10:49:54

标签: leaflet angular-leaflet-directive dc.leaflet.js

我正在使用传单api。我已经为标记添加了自定义控件。

我有一个按钮,可以删除所有标记。

问题

通过包装,我必须删除所有标记,但功能不起作用....浏览器没有给出任何控制台错误,所以我在黑暗中,没有完全理解api的结构。

脚本

<div class="blade ng-scope">


  <div class="list_item ng-scope">
    <a class="item_a ng-binding" tabindex="0">A</a>

    <div class="blade ng-scope">


      <div class="list_item ng-scope">
        <a class="item_a ng-binding" tabindex="0">A1</a>

        <div class="blade ng-scope">


          <div class="list_item ng-scope">
            <a class="item_a ng-binding" tabindex="0">A1-1</a>

            <div class="blade ng-scope">


              <div class="list_item ng-scope">
                <a class="item_a ng-binding" tabindex="0">A1-1 - 1</a>
              </div>

              <div class="list_item ng-scope">
                <a class="item_a ng-binding" tabindex="0">A1-1 - 2</a>
              </div>

              <div class="list_item ng-scope">
                <a class="item_a ng-binding" tabindex="0">A1-1 - 3</a>
              </div>

              <div class="list_item ng-scope">
                <a class="item_a ng-binding" tabindex="0">A1-1 - 4</a>
              </div>
            </div>
          </div>
        </div>
      </div>

      <div class="list_item ng-scope">
        <a class="item_a ng-binding" tabindex="0">A2</a>

        <div class="blade ng-scope">


          <div class="list_item ng-scope">
            <a class="item_a ng-binding" tabindex="0">A2-1</a>
          </div>
        </div>
      </div>

      <div class="list_item ng-scope">
        <a class="item_a ng-binding" tabindex="0">A3</a>
      </div>
    </div>
  </div>

  <div class="list_item ng-scope">
    <a class="item_a ng-binding" tabindex="0">B</a>

    <div class="blade ng-scope">


      <div class="list_item ng-scope">
        <a class="item_a ng-binding" tabindex="0">B1</a>
      </div>

      <div class="list_item ng-scope">
        <a class="item_a ng-binding" tabindex="0">B2</a>
      </div>
    </div>
  </div>

  <div class="list_item ng-scope">
    <a class="item_a ng-binding" tabindex="0">C</a>
  </div>
</div>

如果有人对此有所了解请帮助或任何形式的参考将不胜感激。谢谢您的时间

1 个答案:

答案 0 :(得分:2)

当您正确识别代码时,您可以轻松发现第一个错误:

var markers = new L.FeatureGroup();

map.on('click', function markerPlace(e) {
    //L.marker(e.latlng, { icon: markerIcon, draggable: true }).addTo(map);
    marker = L.marker(e.latlng, { icon: markerIcon, draggable: true }).addTo(map);
    markers.addLayer(marker);
    map.off('click', markerPlace);
});

}).addTo(map); // <- What is this doing here

这应该会给你一个很大的控制台错误。我假设这属于你的L.FeatureGroup

var markers = new L.FeatureGroup().addTo(map); <- Moved to here

map.on('click', function markerPlace(e) {
    //L.marker(e.latlng, { icon: markerIcon, draggable: true }).addTo(map);
    marker = L.marker(e.latlng, { icon: markerIcon, draggable: true }).addTo(map);
    markers.addLayer(marker);
    map.off('click', markerPlace);
});

您希望将标记添加到名为L.FeatureGroup的{​​{1}},然后将其添加到地图中?

markers

应该改为:

marker = L.marker(e.latlng, { icon: markerIcon, draggable: true }).addTo(map);

因此,简而言之:在地图的点击事件中,您尝试从地图中删除标记要素图层,但未正确添加并且您已将标记添加到地图,而不是要素组。

PS:您想删除标记,但实际上您要删除整个功能组。如果您想要保留要素组并且只删除标记,您也可以使用marker = L.marker(e.latlng, { icon: markerIcon, draggable: true }).addTo(markers); 的{​​{1}}方法:

clearLayers