mounted() {
 this.initMap();
},
methods: {
initMap() {
  this.map = L.map('mapContainer').setView([48.856663, 2.351556], 12);
  this.tileLayer = L.tileLayer(
      "https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token={accessToken}",
      {
        attribution: '<a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery (c) <a href="https://www.mapbox.com/">Mapbox</a>',
        maxZoom: 18,
        id: "mapbox/satellite-streets-v11",
        accessToken: "token",
      }
  );
  this.tileLayer.addTo(this.map);
  var drawnItems = new L.FeatureGroup();
  this.map.addLayer(drawnItems);
  var drawControl = new L.Control.Draw({
    edit: {
      featureGroup: drawnItems
    }
  });
  this.map.addControl(drawControl);

  this.map.on(L.Draw.Event.CREATED, function(event){
    let layer = event.layer;
    console.log(`start ${layer}`);
    drawnItems.addLayer((layer))
  })
 },
},
}

leaflet.draw.js?20d6:8 Uncaught TypeError: Cannot read properties of undefined (reading 'length')

After the first interaction with the map, errors appear in the console and it is no longer possible to draw shapes

UPD:Switched to mapbox draw

enter image description here

2

There are 2 best solutions below

1
DKasane On BEST ANSWER

I solved this problem like this. Instead of Leaflet, use MapBox and MapBox draw works in Vue 3

3
auralbee On

I use a similar code to draw a polygon on a basemap and have a similar issue when using Vue3 with leaflet and leaflet-draw. After drawing a polygon, I get a bunch of errors in the console, probably triggered by a mouseover event.

Uncaught TypeError: this._markers is undefined
in leaflet-draw.js:162:18

Haven't found the root cause of the problem yet but the reason might be the fact that leaflet-draw is not in line with the Leaflet Base Library any more (just a guess). The last commit at Github is from 2018 and there is a long list of issues and open pull requests, while Leaflet itself is continously updated.

The problem might also occur when using the library in combination with Vue.

Have you tried creating the above functionality using Vanilla JS?