I have around 1000 points (gps coordinates) which I want to visualize on a map using the Unfolding maps library. The track consists of colored lines (two points). I would like to color the lines based on the speed (if the speed on the track was below 20km/h it should be red otherwise green).

The following code, which represents a custom made line with one color, works:

class MyPolygonMarker extends SimplePolygonMarker {

    public void draw(PGraphics pg, List<MapPosition> mapPositions) {

      pg.pushStyle();
      pg.strokeWeight(2);
      pg.fill(255,0,0,0);
      pg.stroke(#2688AD);
      pg.beginShape();

      for(int i=0; i<mapPositions.size();i++)
      {
        pg.vertex(mapPositions.get(i).x, mapPositions.get(i).y);
      }

      pg.endShape();

      pg.popStyle();
    }
}

So in the for loop I would like to put a if statement which checks the speed value. I tried with this:

class MyPolygonMarker extends SimplePolygonMarker {

public void draw(PGraphics pg, List<MapPosition> mapPositions) {

  pg.pushStyle();
  pg.strokeWeight(2);
  pg.fill(255,0,0,0);

  for(int i=0; i<mapPositions.size();i++)
  {
    Float speed_value = Float.parseFloat(lines[i].split("\t")[7]);

    if(speed_value > 20)
    {
      pg.stroke(green);
      pg.beginShape();
    }
    else
    {
      pg.stroke(red);
      pg.beginShape();
    }
    pg.vertex(mapPositions.get(i).x, mapPositions.get(i).y);
  }
  pg.endShape();
  pg.popStyle();
}
}

This only plots the initial point and nothing else. Could someone tell me what could be wrong?

有帮助吗?

解决方案

It might be simply that you need to put the beginShape() before the for loop.

In any case, we are providing an example which seems to do something really close to what you are trying to achieve:

Take a look at ColoredLinesMarker.java for line markers with colors based on the speed. Note, how in that example we are reading the speed from the properties (you don't have to do it like this, but it is good practice). You can find the full example (including the App and a custom reader for GPX files with speed data) here.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top