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

How to add draw on drawed feature? #1197

Open
maxiwer opened this issue Aug 26, 2023 · 9 comments
Open

How to add draw on drawed feature? #1197

maxiwer opened this issue Aug 26, 2023 · 9 comments
Labels

Comments

@maxiwer
Copy link

maxiwer commented Aug 26, 2023

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?
@kumilange
Copy link

@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

@maxiwer
Copy link
Author

maxiwer commented Oct 11, 2023

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

@kumilange
Copy link

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

@maxiwer
Copy link
Author

maxiwer commented Oct 15, 2023

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?

@kumilange
Copy link

@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).

@derwaldgeist
Copy link

derwaldgeist commented Dec 27, 2023

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
Copy link

derwaldgeist commented Dec 27, 2023

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.

@Nikhil22
Copy link

Nikhil22 commented Jan 17, 2024

@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.

@kumilange
Copy link

kumilange commented Jan 19, 2024

@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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants