Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Need a check if maplibre source already exists on draw control #192

Open
saricicekibrahim opened this issue Feb 6, 2025 · 1 comment
Labels
bug Something isn't working

Comments

@saricicekibrahim
Copy link

saricicekibrahim commented Feb 6, 2025

Select items which related to this issue

maplibre-gl-terradraw

Bug Description

Using both a trigger to draw on the map and a draw control:

  let isDrawing = false;

  // Setup for TerraDraw
  const draw = new TerraDraw({
    adapter: new TerraDrawMapLibreGLAdapter({ map: map.current }),
    modes: [new TerraDrawRectangleMode()],
  });

  // Function to toggle drawing
  function startDrawing() {
    if (!isDrawing) {
      draw.start();
      draw.setMode("rectangle");
      isDrawing = true; // Set the drawing state to true
    }
  }

  // Call startDrawing when you need to initiate drawing
  startDrawing();

And draw control

  drawControl = new MaplibreTerradrawControl({
    modes: ['render', 'point', 'linestring', 'polygon', 'rectangle', 'circle', 'freehand', 'angled-rectangle', 'sensor', 'sector', 'select', 'delete-selection', 'download'],
    open: true,
  });

 map.current.addControl(drawControl);

After using the draw trigger, when I try to add a polygon with the draw control, I encounter an error: "Source 'td-point' already exists."
It may be necessary to check if the source has already been added to MapLibre on draw control.

Bug Reproduction

No response

Bug Screenshots

No response

Bug System Information

  • OS: [e.g. Windows 10]
  • Browser: [e.g. Google Chrome, Firefox]
@saricicekibrahim saricicekibrahim added the bug Something isn't working label Feb 6, 2025
@JinIgarashi
Copy link
Member

@saricicekibrahim it is not possible to use two terradraw instances together.

You can use getTerradrawInstance method from the plugin to do your own processing for terradraw.

https://watergis.github.io/maplibre-gl-terradraw/classes/controls_MaplibreTerradrawControl.MaplibreTerradrawControl.html#getterradrawinstance

For example,

  drawControl = new MaplibreTerradrawControl({
    modes: ['render', 'point', 'linestring', 'polygon', 'rectangle', 'circle', 'freehand', 'angled-rectangle', 'sensor', 'sector', 'select', 'delete-selection', 'download'],
    open: true,
  });

 map.current.addControl(drawControl);

terradraw = drawControl.getTerradrawInstance()
// do your own coding and settings for terradraw instance itself.

you can subscribe events from terradraw directly, or plugin also has own on method to subscribe an event.

https://watergis.github.io/maplibre-gl-terradraw/classes/controls_MaplibreTerradrawControl.MaplibreTerradrawControl.html#on

currently the plugin only has two types of events ( "mode-changed" | "feature-deleted"), but feel free to make a PR to add other type of event type to the plugin.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants