Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

173
Vistas
Different layer styles affecting each other openlayers

I want to draw dashed line and straight line by using open layers Draw method. In this method there is a LineString option for drawing straight line but I cound not find a option for dashed line. So my aim is to style LineString and make a dashed line. But the problem is, this approach is also affecting the straight line style even though I'm keeping them in different layers.

import map from "./main.js";
const lineString = document.querySelector("#lineString");
const lineDashed = document.querySelector("#lineDashed");
let checkString = false;
let checkDashed = false;

const lineStringSource = new ol.source.Vector();
let stringLayer = new ol.layer.Vector({
  source: lineStringSource,
  style: new ol.style.Style({
    stroke: new ol.style.Stroke({
      color: "#ffcc33",
      width: 2,
    }),
  }),
});
const dashStringSource = new ol.source.Vector();
let dashLayer = new ol.layer.Vector({
  source: dashStringSource,
  style: new ol.style.Style({
    stroke: new ol.style.Stroke({
      width: 2,
      lineDash: [1, 20],
    }),
  }),
});
map.addLayer(stringLayer);
map.addLayer(dashLayer);
lineString.addEventListener("click", () => {
  if (checkString === false) {
    lineString.checked = true;
    lineDashed.checked = false;
    checkDashed = false;
    checkString = true;
    addInteraction(lineStringSource);
  } else {
    lineString.checked = false;
    lineDashed.checked = false;
    checkString = false;
    checkDashed = false;
  }
});
lineDashed.addEventListener("click", () => {
  if (checkDashed === false) {
    lineString.checked = false;
    lineDashed.checked = true;
    checkDashed = true;
    checkString = false;
    addInteraction(dashStringSource);
  } else {
    lineString.checked = false;
    lineDashed.checked = false;
    checkDashed = false;
  }
});
let drawStringLine, drawDashLine;
function addInteraction(sourceType) {
  if (sourceType === lineStringSource) {
    drawStringLine = new ol.interaction.Draw({
      source: sourceType,
      type: "LineString",
    });
    map.addInteraction(drawStringLine);
  } else {
    drawDashLine = new ol.interaction.Draw({
      source: sourceType,
      type: "LineString",
    });
    map.addInteraction(drawDashLine);
  }
}

LineString and lineDashed are input radio. Whenever user select one of this input type I want them to be able to draw that option. But the above code problem is when I select lineDash it is also yellow just like lineString. And if I select lineDash and then select lineString again this time lineString have a dash style on it. This issue is also affecting the lines already on the map.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You need to remove the old interaction before creating and adding a new one

let drawStringLine, drawDashLine;
function addInteraction(sourceType) {
  if (sourceType === lineStringSource) {
    map.removeInteraction(drawDashLine);
    drawStringLine = new ol.interaction.Draw({
      source: sourceType,
      type: "LineString",
    });
    map.addInteraction(drawStringLine);
  } else {
    map.removeInteraction(drawStringLine);
    drawDashLine = new ol.interaction.Draw({
      source: sourceType,
      type: "LineString",
    });
    map.addInteraction(drawDashLine);
  }
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda