简体   繁体   English

快速更改绘图工具

[英]Changing the drawing instruments on a fly

I have adjusted openlayers basic draw example and whenever I am switching from Point to LineString or Polygon drawing mode, it is working properly only for the first time , yet fails for switching overtimes . 我已经调整了openlayers基本绘图示例 ,每当我从Point 切换LineStringPolygon绘图模式时,它只能在第一次正常运行,但无法切换加班时间

FI I cannot get back to drawing Point after using LineString (it continues to draw me LineString, yet log prints that drawingtool is Point ) FI我在使用LineString之后无法返回到绘图Point (它继续为我绘制LineString,但日志打印出drawingtool是Point

My html for buttons in modal: 我的HTML模态按钮:

 <div class="modal-body row">
    <div class="col-xs-12 col-sm-3">
      <a id="Point" href="#" class="draw btn btn-primary btn-block"><span class="icon ion-android-pin"></span>  Point</a>
    </div>
    <div class="col-xs-12 col-sm-3">
      <a id="LineString" href="#" class="draw btn btn-primary btn-block"><span class="icon ion-android-pin"></span>  Polyline</a>
    </div>
    <div class="col-xs-12 col-sm-3">
      <a id="Polygon" href="#" class="draw btn btn-primary btn-block"><span class="ion ion-ios-grid-view-outline"></span>  Polygon</a>
    </div>
    <div class="col-xs-12 col-sm-3">
      <a href="#" class="btn btn-primary btn-block"><span class="ion ion-pinpoint"></span>  Coordinates</a>
    </div>
  </div>
</div>

And here is part of js: 这是js的一部分:

function addInteraction() {
  draw = new ol.interaction.Draw({
    features: features,
    type: /** @type {ol.geom.GeometryType} */ (drawtool)
  });
  map.addInteraction(draw);
}
$('.draw').on("click", function(e) {
  //e.preventDefault(); - commented by me - no difference
  drawtool = $(this).attr('id');
  $("#drawModal").modal('toggle');
  addInteraction();
});

You can also find whole code on jsFiddle 您还可以在jsFiddle上找到整个代码

The problem is that everytime you are calling addInteraction() you are adding a new instance of ol.interaction.Draw to the map, so the existing one is never removed. 问题在于,每次调用addInteraction()时,都会在地图上添加ol.interaction.Draw的新实例,因此不会删除现有实例。

The quick and dirty solution is to remove the existing interaction before creating the new one. 快速而肮脏的解决方案是在创建新交互之前删除现有交互。

Just add 只需添加

map.removeInteraction(draw);

before 之前

draw = new ol.interaction.Draw(...

See the updated fiddle https://jsfiddle.net/zuarah4t/3/ 看到更新的小提琴https://jsfiddle.net/zuarah4t/3/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM