faced with the situation when ol draws lines if the coordinates are "scattered". for example.

coords as is: coords as is and sorted coords by date: sorted coords bottom image is almost correct except sharp lines up. sometimes the gsm signal may disappear and the coordinates are recorded in the memory of the transmitting device and as soon as a signal appears, the unsent coordinates are transmitted to the server.

how can i fix it? how to make openlayers not draw sharp lines?


You could calculate the angles at each vertex on the linestring and remove any where the angle is sharper than your requirement. A function such as this would do it.

  function checkAngles(feature, minAngle) {
      var previous, previousAngle;
      var angles = [];
      var coordinates = feature.getGeometry().getCoordinates();
      feature.getGeometry().getCoordinates().forEach( function(coordinate, index) {
          if (previous) {
              var x = (previous[0] - coordinate[0]);
              var y = (previous[1] - coordinate[1]);
              var angle = Math.atan(x/y);
              angle = y > 0 ? angle + Math.PI : x < 0 ? angle + Math.PI*2 : angle;
              if (previousAngle) {
              previousAngle = angle;
          previous = coordinate;
      for (i = angles.length; i > 0; i--) {
           if (Math.abs(angles[i-1]) < minAngle) {
      feature.setGeometry(new ol.geom.LineString(coordinates));    

Here a demo where you can draw a linestring. Any angles sharper than 10 degrees will be removed. http://mikenunn.16mb.com/demo/linestring-angles.html

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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