mapbox-gl-draw icon indicating copy to clipboard operation
mapbox-gl-draw copied to clipboard

How to add draw on drawed feature?

Open maxiwer opened this issue 1 year ago • 9 comments

Hello mapbox-gl-draw team. How can I add a layer to draw object? I've tried this:

let ftr = {
      type: 'Feature',
      geometry: {
        type: 'Polygon',
        coordinates: layer.coordinates[0],
      },
      properties: []
    };
    let featureIds = this.draw.add(ftr as Feature);
    ```
    but it throws an error: `ERROR TypeError: Cannot read properties of undefined (reading 'get')`. What's the problem?

maxiwer avatar Aug 26 '23 07:08 maxiwer

@maxiwer Hi, I can confirm that add behavior works (I'm not a maintainer for this library by the way).

Here is what the code looks like: Screenshot 2023-09-13 at 16 43 36

API reference https://github.com/mapbox/mapbox-gl-draw/blob/main/docs/API.md

kumilange avatar Sep 13 '23 22:09 kumilange

@kumiko-haraguchi Is it just a layer? Where's the drawing tool's points?

maxiwer avatar Oct 11 '23 16:10 maxiwer

@maxiwer You can click the polygon and play with it in the same way as drawing it with the tool.

kumilange avatar Oct 11 '23 17:10 kumilange

I don't understand where I'm getting this wrong. Could you please review this: Here I'm toggling "draw" tool when editing mode is on:

        if (isEditing) {
          this.map?.addControl(this.draw as any);
          this.test();
        }

and here I am trying to add kind of what you've wrote here:

  private test() {
    const layer = JSON.parse(this.currentLayer$.getValue().geom) as GeomModel;
    this.map?.on('load', () => {
      this.draw.add({
        id: 'sample-id',
        type: 'Feature',
        properties: [],
        geometry: {
          type: 'Polygon',
          coordinates: layer.coordinates[0],
        }
      });
    })
  }

What's wrong?

maxiwer avatar Oct 15 '23 18:10 maxiwer

@maxiwer Hi, it's hard to tell why your code isn't working from your code snippets.

Would you be able to provide your code by using jsbin like this? https://jsbin.com/suqewac/edit?html,output You can check the working code(don't forget to add your mapbox acccess token).

kumilange avatar Oct 20 '23 15:10 kumilange

I'm getting the same error as the OP. Even the sample in the API doc won't work for me. No matter what I try (the Feature seems to be correct), I am getting: TypeError: Cannot read properties of undefined (reading 'get')

derwaldgeist avatar Dec 27 '23 19:12 derwaldgeist

I tried to debug this, and it seems as if ctx.store is not defined here:

https://github.com/mapbox/mapbox-gl-draw/blob/main/src/api.js#L85

I am using mapbox-gl 1.x, since I am using it in combination with MapTiler and Urbica's react-map-gl implementation. Maybe map-gl-draw is not compatible with 1.x anymore?

EDIT: I tested with mapbox-gl 3.x and am seeing the same results.

derwaldgeist avatar Dec 27 '23 19:12 derwaldgeist

@derwaldgeist @maxiwer I was getting the exact same error, but I got it working by ensuring that map has the draw control, before trying to call draw.whatever().

Example:

  useEffect(() => {
    if (!draw?.current || !hasDrawControl) return
    draw.current.set({
      type: 'FeatureCollection',
      features
    })
  }, [draw, hasDrawControl])

hasDrawControl is simply a useState variable that is set like this:

      map.addControl(draw.current);
      setHasDrawControl(true)

Works because map.addControl is synchronous, as shown here too.

Nikhil22 avatar Jan 17 '24 11:01 Nikhil22

@maxiwer @derwaldgeist I tried to reproduce the error by writing it in a Class but it seems to work fine for me...(confirmed with mapbox-gl-js 1.x, 2.x and 3.x | mapbox-gl-draw v1.4.3). I'm not sure how we are doing differently. If you could provide the code that reproduces the error, that would be helpful.

class MapboxGlDrawManager {
  constructor() {
    mapboxgl.accessToken = 'MAPBOX_ACCESS_TOKEN';

    this.map = new mapboxgl.Map({
      container: 'map',
      style: 'mapbox://styles/mapbox/light-v11',
      center: [-68.137343, 45.137451],
      zoom: 5
    });

    this.draw = new MapboxDraw({
      controls: {
        polygon: true,
        trash: true
      },
    });

    this.init();
  }

  init() {
    this.map.addControl(this.draw);

    this.map.on('load', () => {
      this.draw.add({
        type: 'FeatureCollection',
        features: [
          {
            type: 'Feature',
            geometry: {
              type: 'Polygon',
              coordinates: // omit
            }
          },
        ],
      });
    });
  }
}

// Initialize MapboxGlDrawManager
const initialize = () => new MapboxGlDrawManager();

if (document.readyState === "loading") {
  document.addEventListener("DOMContentLoaded", initialize);
} else {
  initialize();
}

@stepankuzmin Were you able to verify this bug?

kumilange avatar Jan 19 '24 16:01 kumilange