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 coordinates = feature.getGeometry().getCoordinates();
    var multi = (feature.getGeometry().getType() == 'MultiLineString');
    var multiCoordinates = multi ? coordinates : [coordinates];
    multiCoordinates.forEach( function(lineCoordinates) {
      var previous, previousAngle;
      var angles = [];
      lineCoordinates.forEach( function(coordinate) {
          if (previous) {
              var x = (previous[0] - coordinate[0]);
              var y = (previous[1] - coordinate[1]);
              var angle = Math.atan2(x,y);
              if (previousAngle) {
              previousAngle = angle;
          previous = coordinate;
      for (i = angles.length; i > 0; i--) {
           if (Math.abs(angles[i-1]) < 10) {
    feature.setGeometry( multi ? new ol.geom.MultiLineString(coordinates) : new ol.geom.LineString(coordinates) );    

Here's 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

  • how can this be applied to multiline-string? – adshfadf1244 Jan 24 at 16:04
  • I've updated the function to handle both linestrings and multilinestrings (where the coordinates are arrays of linestrings). – Mike Jan 24 at 19:06
  • Mike, help me pls again. ibb.co/9gj3bny in blue, i marked the wrong lines. How to remove them when building a line? – adshfadf1244 Jan 25 at 12:15
  • Is that because the signal was lost and some of the coordinates are missing? I think all you can do is don't include lines which don't have a minimum number of coordinates for your route. – Mike Jan 25 at 15:58

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

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