1

I learning to drawing Polygon with code below:

var markersAreaGroup = L.layerGroup();
markersAreaGroup.addTo(map);

var tooltipAreaGroup = L.layerGroup();
tooltipAreaGroup.addTo(map);

var lineAreaGroup = L.layerGroup();
lineAreaGroup.addTo(map);

var markerArea;
var markersAreaCount;
var lineArea;
const coordinatesArea = [];
const latArea = [];
const longArea = [];

map.on('click', function(e){
markerArea = new L.marker(e.latlng, {alt: 'markerArea', icon: rulerIcon}).on('click',function(e) {
   event.stopPropagation();
});
markersAreaGroup.addLayer(markerArea);
      
markersAreaCount = markersAreaGroup.getLayers().length;

coordinatesArea.push([e.latlng.lat,e.latlng.lng]);

//draw area
lineArea = L.polygon(coordinatesArea, {
      alt : 'lineArea',
      weight: 1,
      color: '#090dff',
      fillColor: '#090dff',
      fillOpacity: 0.2,
      });

      //add the area
      lineAreaGroup.addLayer(lineArea);
});

When draw with map click (example 4 markers), I got stacked result as image below:

enter image description here

What's wrong? I just expect the result like this example below:

enter image description here

5
  • it feels like you are drawing a polygon with your point list after each click event on the map. So with 4 clicks , you have a point then a line, then a triangle and then a 4 point polygon. You need to be carefull with how your code work and check the content of each object when you trigger an event. with some console.log here you should be able to get what happen Commented Oct 21 at 14:43
  • console.log result seems ok. Array(4) [ (2) […], (2) […], (2) […], (2) […] ] 0: Array [ 6.055501318209942, 101.51663221974532 ] 1: Array [ 2.63945830109756, 103.49472404371227 ] 2: Array [ 4.261807178832384, 109.42899951561314 ] 3: Array [ 7.582073029875818, 107.53882288382249 ] length: 4 Coordinates has right. Maybe you have suggestions?
    – dvaa
    Commented Oct 21 at 14:58
  • the list in the end may be right, but when you are clicking and drawing the polygon it produces the other polygon Commented Oct 21 at 15:28
  • 1
    Each time you click on map, your code creates new polygon from all existing points and places it above previously created polygons. What would you actually like to happen? With each click just to add point to previously created polygon? Please edit your question and clarify this.
    – TomazicM
    Commented Oct 21 at 20:38
  • I have already edit my question. Hope you got my expectation result
    – dvaa
    Commented Oct 22 at 1:14

1 Answer 1

1

If you want to create only one polygon and add points to it when clicking on the map, then one possible way of doing that is to define empty polygon outside click processing function and then on map click just set points of this polygon with .setLatLngs method.

Relevant part of the code would then look something like this:

var lineArea = L.polygon([], {
  alt : 'lineArea',
  weight: 1,
  color: '#090dff',
  fillColor: '#090dff',
  fillOpacity: 0.2,
});        
lineAreaGroup.addLayer(lineArea);

var markerArea;
var markersAreaCount;
var lineArea;
const coordinatesArea = [];
const latArea = [];
const longArea = [];

map.on('click', function(e) {
  markerArea = new L.marker(e.latlng, {alt: 'markerArea', icon: rulerIcon}).on('click',function(e) {
     event.stopPropagation();
  });
  markersAreaGroup.addLayer(markerArea);              
  markersAreaCount = markersAreaGroup.getLayers().length;
  coordinatesArea.push([e.latlng.lat,e.latlng.lng]);
  lineArea.setLatLngs(coordinatesArea);
});
1
  • Work! Thanks and have nice day!
    – dvaa
    Commented Oct 22 at 11:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.