3
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

DKasane
  • 51
  • 4

2 Answers2

1

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

DKasane
  • 51
  • 4
0

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?

auralbee
  • 8,741
  • 4
  • 29
  • 36
  • Thanks for the answer No, I haven't tried to do it on Vanilla JS, I need to implement drawing objects on the map on VUE 3, in fact this problem is not critical, since the functionality itself works (drawing polygons, polygons, etc.) But now I have another difficulty, I don't understand how to store the data that I received from the user so that the delete button would work, I understand that each new polyline or polygon is a new layer, but how do I then convert it to GeoJSON, I didn't find in the documentation how to get the current objects on the map – DKasane Dec 27 '21 at 01:59
  • I have been using leaflet and I have also faced this issue when using vue3 but works well with vue2. If you found the solution kindly give some pointers – Barty Jan 29 '22 at 11:08
  • @DKasane I have the problem in VueJS3 but with the same code I haven't the error. The error is bound to VueJS. – Giildo Jul 01 '22 at 13:00