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

688
Views
openlayers, drawing clouds, polygon with customized stroke

I am working with openlayers 6 and I need to draw clouds on the maps a polygon with half circles stroke or a linestring, it doesn't matter the type as long as I can modify it ( adding and removing points, stretching, shrinking it). My knowledge of Openlayers is very limited so I am asking for guidance, how can I possibly do that

this image containes and example of what I want to achieve, an editable cloud shape drawn by a user

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You could use a custom style which changes the displayed geometry, similar to https://openlayers.org/en/latest/examples/polygon-styles.html but replacing the segments of the polygon ring with semi-circular arcs.

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.14.1/css/ol.css" type="text/css">
    <style>
      html, body, .map {
        margin: 0;
        padding: 0;
        width: 100%;
        height: 100%;
      }
    </style>
    <script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.14.1/build/ol.js"></script>
  </head>
  <body>
    <div id="map" class="map"></div>
    <script>
      var raster = new ol.layer.Tile({
        source: new ol.source.OSM()
      });

      var source = new ol.source.Vector({wrapX: false});

      var style = new ol.style.Style({
        stroke: new ol.style.Stroke({
          color: 'blue',
          width: 3
        }),
        fill: new ol.style.Fill({
          color: 'rgba(0, 0, 255, 0.1)'
        }),
        geometry: function (feature) {
          var geometry = feature.getGeometry();
          if (geometry.getType() === 'Polygon') {
            var coordinates = geometry.getCoordinates(true)[0];
            var arcs = [];
            for (let i = 0, len = coordinates.length - 1; i < len; ++i) {
              var center = [
                (coordinates[i + 1][0] + coordinates[i][0]) / 2,
                (coordinates[i + 1][1] + coordinates[i][1]) / 2
              ];
              var dx = coordinates[i + 1][0] - coordinates[i][0];
              var dy = coordinates[i + 1][1] - coordinates[i][1];
              var radius = Math.sqrt(dx * dx + dy * dy) / 2;
              var angle = Math.atan2(-dy, -dx);
              var sides = 32;
              arcs = arcs.concat(
                ol.geom.Polygon.fromCircle(
                  new ol.geom.Circle(center, radius),
                  sides,
                  angle
                ).getCoordinates(true)[0].slice(0, sides / 2)
              );
            }
            return new ol.geom.Polygon([arcs.concat([arcs[0]])]);
          } else {
            return geometry;
          }
        }
      });

      var vector = new ol.layer.Vector({
        source: source,
        style: style
      });

      var map = new ol.Map({
        layers: [raster, vector],
        target: 'map',
        view: new ol.View({
          center: [-11000000, 4600000],
          zoom: 4
        })
      });

      var modify = new ol.interaction.Modify({source: source});
      map.addInteraction(modify);

      var draw = new ol.interaction.Draw({
        source: source,
        type: 'Polygon',
        style: style
      });
      map.addInteraction(draw);

      var snap = new ol.interaction.Snap({source: source});
      map.addInteraction(snap);

    </script>
  </body>
</html>

about 4 years ago · Juan Pablo Isaza Report
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!