Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

306
Views
How to draw polyline with mouse clicks in React-Leaflet?

I want to add a line to previously created line with clicks on the map. But everytime i click on the map, previously created lines are deleted.

import React, { useEffect, useState } from 'react';
import { Polyline, useMap} from 'react-leaflet';
import L from 'leaflet';

let previousPoint;

let points;

 const DrawLines=()=> {

  const [Points,setPoints]=useState([])
  const [drawLine,setDrawLine]=useState(false);  

  const map =useMap();
  useEffect(() => {
    if (!map) return;
    map.on('click', (e) => {

      if(previousPoint != null){
        points=[[previousPoint.lat, previousPoint.lng],
        [e.latlng.lat, e.latlng.lng]]
        setPoints(points);
        setDrawLine(true) 
      }
      previousPoint = e.latlng;     
    })

    map.addControl(new positon());

  }, [map])

  return (
    <>
    {
      drawLine&&points&&
       <Polyline
      positions={Points} weight={10} opacity={0.7} color='red'>
    </Polyline>
    }
 
    </>
   )
}

export default DrawLines;

I tried to store all points i clicked then draw polyline, but it didnt work. I want to keep previous state of the map. I believe there must be a way to do this. Any help is greatly appreciated. Thanks in advance.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!