Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

I am experimenting with openlayers. I am adding points to the map using this example. However, I also want to delete unwanted points from the map. For example, I clicked 6 points on the map and I want to remove the fifth point, but keep 1, 2, 3, 4, and 6 th point on the map. How can I do this in openLayers. So far, I tried this exampleexample and this example, but I had no luck so far. Please help me out.

I am experimenting with openlayers. I am adding points to the map using this example. However, I also want to delete unwanted points from the map. For example, I clicked 6 points on the map and I want to remove the fifth point, but keep 1, 2, 3, 4, and 6 th point on the map. How can I do this in openLayers. So far, I tried this example and this example, but I had no luck so far. Please help me out.

I am experimenting with openlayers. I am adding points to the map using this example. However, I also want to delete unwanted points from the map. For example, I clicked 6 points on the map and I want to remove the fifth point, but keep 1, 2, 3, 4, and 6 th point on the map. How can I do this in openLayers. So far, I tried this example and this example, but I had no luck so far. Please help me out.

added 91 characters in body
Source Link
WOW
  • 327
  • 1
  • 3
  • 8

Update 2: I can remove the last element using removeLastPoint() inside ol.interaction.Draw.

Update 2: I can remove the last element using removeLastPoint() inside ol.interaction.Draw.

Post Reopened by underdark
added 1921 characters in body
Source Link
underdark
  • 84.5k
  • 22
  • 233
  • 415
var raster = new ol.layer.Tile({
    source: new ol.source.OSM()
  });

  var map = new ol.Map({
    layers: [raster],
    target: 'map',
    view: new ol.View({
      center: [-11000000, 4600000],
      zoom: 4
    })
  });

  var features = new ol.Collection();
  var featureOverlay = new ol.layer.Vector({
    source: new ol.source.Vector({features: features}),
    style: new ol.style.Style({
      fill: new ol.style.Fill({
        color: 'rgba(255, 255, 255, 0.2)'
      }),
      stroke: new ol.style.Stroke({
        color: '#ffcc33',
        width: 2
      }),
      image: new ol.style.Circle({
        radius: 7,
        fill: new ol.style.Fill({
          color: '#ffcc33'
        })
      })
    })
  });
  featureOverlay.setMap(map);

  var modify = new ol.interaction.Modify({
    features: features,
    // the SHIFT key must be pressed to delete vertices, so
    // that new vertices can be drawn at the same position
    // of existing vertices
    deleteCondition: function(event) {
      return ol.events.condition.shiftKeyOnly(event) &&
          ol.events.condition.singleClick(event);
    }
  });
  map.addInteraction(modify);

  var draw; // global so we can remove it later
  var typeSelect = document.getElementById('type');

  function addInteraction() {
    draw = new ol.interaction.Draw({
      features: features,
      type: /** @type {ol.geom.GeometryType} */ (typeSelect.value)
    });
    map.addInteraction(draw);
  }


  /**
   * Handle change event.
   */
  typeSelect.onchange = function() {
    map.removeInteraction(draw);
    addInteraction();
  };

  addInteraction();
var raster = new ol.layer.Tile({
    source: new ol.source.OSM()
  });

  var map = new ol.Map({
    layers: [raster],
    target: 'map',
    view: new ol.View({
      center: [-11000000, 4600000],
      zoom: 4
    })
  });

  var features = new ol.Collection();
  var featureOverlay = new ol.layer.Vector({
    source: new ol.source.Vector({features: features}),
    style: new ol.style.Style({
      fill: new ol.style.Fill({
        color: 'rgba(255, 255, 255, 0.2)'
      }),
      stroke: new ol.style.Stroke({
        color: '#ffcc33',
        width: 2
      }),
      image: new ol.style.Circle({
        radius: 7,
        fill: new ol.style.Fill({
          color: '#ffcc33'
        })
      })
    })
  });
  featureOverlay.setMap(map);

  var modify = new ol.interaction.Modify({
    features: features,
    // the SHIFT key must be pressed to delete vertices, so
    // that new vertices can be drawn at the same position
    // of existing vertices
    deleteCondition: function(event) {
      return ol.events.condition.shiftKeyOnly(event) &&
          ol.events.condition.singleClick(event);
    }
  });
  map.addInteraction(modify);

  var draw; // global so we can remove it later
  var typeSelect = document.getElementById('type');

  function addInteraction() {
    draw = new ol.interaction.Draw({
      features: features,
      type: /** @type {ol.geom.GeometryType} */ (typeSelect.value)
    });
    map.addInteraction(draw);
  }


  /**
   * Handle change event.
   */
  typeSelect.onchange = function() {
    map.removeInteraction(draw);
    addInteraction();
  };

  addInteraction();
Added a Fiddle that shows my code
Source Link
WOW
  • 327
  • 1
  • 3
  • 8
Loading
Post Closed as "Not suitable for this site" by underdark
Source Link
WOW
  • 327
  • 1
  • 3
  • 8
Loading