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

268
Views
React Openlayers, Polygon is removing after drawend

I'm using openlayers in react project. And trying to draw a polygon. When i compelete to draw, polygon is removing. I believe clicking to added coordinate is causing that. But cannot find any solution.

i follow this article for creating map. and this example for drawing polygon.

here is my map component:

    <MapWrapper center={fromLonLat(DEFAULT_CENTER)} zoom={ZOOM}>
      <MapLayers>
        <MapTileLayer source={new OSM()} />
      </MapLayers>
      <Controls>
        <ZoomControl />
        <FullScreenControl />
      </Controls>
    </MapWrapper>

and this is my map wrapper component:

const MapWrapper = ({ center, zoom, children }) => {
  const mapRef = useRef();
  const [map, setMap] = useState();
  const source = new VectorSource({ wrapX: false });
  //----------------ON COMPONENT MOUNT
  useEffect(() => {
    let options = {
      view: new ol.View({ zoom, center }),
      layers: [],
      controls: [],
      overlays: [],
    };
    let mapObject = new ol.Map(options);
    mapObject.setTarget(mapRef.current);
    setMap(mapObject);
    return () => mapObject.setTarget(undefined);
  }, [zoom, center]);
  //------------ DRAWING POLYGON -----------------
  useEffect(() => {
    if (!map) return;
    let draw = new Draw({
      source: source,
      type: "Polygon",
    });
    map.addInteraction(draw);
  }, [map]);
  return (
    <MapContext.Provider value={{ map }}>
      <div className="map-container"
        ref={mapRef}
      >
        {children}
      </div>
    </MapContext.Provider>
  );
};

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!