I have this code, which plots on a map a linestring which is the track of 2 coordinate points that the user supplies.
public class Quickstart {
public stati
For the style you need something like the SLD described here, in code that becomes:
// Create style for the line
// Style style = SLD.createSimpleStyle(TYPE, Color.red);
org.geotools.styling.Style style = SLD.createLineStyle(Color.red, 2.0f);
StyleBuilder sb = new StyleBuilder();
FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2();
PointSymbolizer point = sb.createPointSymbolizer();
Mark mark = sb.createMark("shape://oarrow");
mark.setFill(sb.createFill(Color.RED));
mark.setStroke(sb.createStroke(Color.red));
Graphic graphic = sb.createGraphic(null, mark, null);
graphic.setRotation(ff.function("endAngle", ff.property("line")));
point.setGraphic(graphic);
point.setGeometry(ff.function("endpoint", ff.property("line")));
Rule rule = sb.createRule(point);
style.getFeatureTypeStyles()[0].addRule(rule );
Layer layer = new FeatureLayer(featureCollection, style);
Zooming into the line is just a case of setting the map viewport to the bounds of the line:
MapViewport viewport = new MapViewport(featureCollection.getBounds());
map.setViewport(viewport );
If you want you might want to grow those bounds by a little (10%?) so that you can see the surroundings too.
EDIT
To avoid the deprecated methods in StyleBuilder
you can use:
style.featureTypeStyles().get(0).rules().add(rule);
Expanding the bounding box is just a case of adding some distance to the envelope:
ReferencedEnvelope bounds = featureCollection.getBounds();
double delta = bounds.getWidth()/20.0; //5% on each side
bounds.expandBy(delta );
MapViewport viewport = new MapViewport(bounds);
map.setViewport(viewport );