Search code examples
buttonopenlayersdraw

Openlayers draw button


This example shows a drawing feature upon a button press. It allows you to select different shapes such as points, lines, polygons, etc. I want to incorporate similar functions into my project. But I don't want to select shapes, instead, I want to press the button to turn on the functions of drawing points, lines, polygons, etc.

I assume that button elements should be inserted in the HTML section.

<select id="type">
<option value="Point">Point</option>
<option value="LineString">LineString</option>
<option value="Polygon">Polygon</option>
<option value="Circle">Circle</option>
<option value="None">None</option>
</select>

I tried as below.

 </div>
<div id="type">
<button value="Point">button1</button>
<button value="Polygon">button2</button>
<button value="LineString">button3</button>
</div>

It seems to me that something needs to change in the script as well.

var typeSelect = document.getElementById('type');

var draw; // global so we can remove it later
function addInteraction() {
var value = typeSelect.value;
if (value !== 'None') {
draw = new Draw({
source: source,
type: typeSelect.value,
});
map.addInteraction(draw);
}
}

Can somebody please help me?


Solution

    1. Change the select to buttons.
        <div id="type">
          <button id="Point" value="Point">Point</button>
          <button id="Polygon" value="Polygon">Polygon</button>
          <button id="LineString" value="LineString">LineString</button>
          <button id="None" value="None">None</button>
        </div>
    
    1. Add a click listener to each button to set the interaction appropriately
      /**
       * Handle button clicks.
       */
      function handleBtnClick() {
        var element=this;
        map.removeInteraction(draw);
        addInteraction(element);
      };
      document.getElementById("Point").addEventListener('click', handleBtnClick);
      document.getElementById("Polygon").addEventListener('click', handleBtnClick);
      document.getElementById("LineString").addEventListener('click', handleBtnClick);
      document.getElementById("None").addEventListener('click', handleBtnClick);
    

    working example

    screenshot of map with Point, Polyline and LineString

    code snippet:

    var raster = new ol.layer.Tile({ // TileLayer({
      source: new ol.source.OSM(),
    });
    
    var source = new ol.source.Vector({ // VectorSource({
      wrapX: false
    });
    
    
    var vector = new ol.layer.Vector({ // VectorLayer({
      source: source,
    });
    
    var map = new ol.Map({
      layers: [raster, vector],
      target: 'map',
      view: new ol.View({
        center: [-11000000, 4600000],
        zoom: 4,
      }),
    });
    
    var typeSelect = document.getElementById('type');
    
    var draw; // global so we can remove it later
    function addInteraction(element) {
      var value = element.value;
      if (value !== 'None') {
        draw = new ol.interaction.Draw({
          source: source,
          type: value,
        });
        map.addInteraction(draw);
      }
    }
    
    /**
     * Handle button clicks.
     */
    function handleBtnClick() {
      var element = this;
      map.removeInteraction(draw);
      addInteraction(element);
      //    return false;
    };
    document.getElementById("Point").addEventListener('click', handleBtnClick);
    document.getElementById("Polygon").addEventListener('click', handleBtnClick);
    document.getElementById("LineString").addEventListener('click', handleBtnClick);
    document.getElementById("None").addEventListener('click', handleBtnClick);
    
    addInteraction(document.getElementById('None'));
    html,
    body {
      height: 100%;
      width: 100%;
      padding: 0px;
      margin: 0px;
    }
    
    .map {
      height: 90%;
      width: 100%;
    }
    <title>Draw Features</title>
    <link rel="stylesheet" href="https://openlayers.org/en/v6.4.3/css/ol.css" type="text/css">
    <!-- Pointer events polyfill for old browsers, see https://caniuse.com/#feat=pointer -->
    <script src="https://unpkg.com/elm-pep"></script>
    <script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.4.3/build/ol.js"></script>
    <div id="map" class="map"></div>
    <label>Geometry type &nbsp;</label>
    <div id="type">
      <button id="Point" value="Point">Point</button>
      <button id="Polygon" value="Polygon">Polygon</button>
      <button id="LineString" value="LineString">LineString</button>
      <button id="None" value="None">None</button>
    </div>