Adding Polylines in World Wind Java SDK
In this article we will look at how polylines can be added to World Wind Java SDK. Polylines are used to represent routes or tracks on your map. A polyline is made up of a list of point coordinates and a line is drawn between each point to create the full polyline. There are actually two different types of Polylines in WWJ. We will take a look at both of them. Note: this tutorial is just to get you pointed in the right direction. There are probably much more efficient ways of handling this. This is just a starting place.
The most simple to use is the SurfacePolyline. This is because it does not require an elevation for each point. It simply draws the line flat against the layer. The surface polyline takes a list of LatLon classes. The LatLon class, as the name implies, is a 2 dimensional point constructed of latitude and longitude. To make a polyline we must first store the points that make up the line.
// Equator line
List<LatLon> latLonList = new ArrayList<LatLon>();
latLonList.add(LatLon.fromRadians(0.0, -180.0));
latLonList.add(LatLon.fromRadians(0.0, 90.0));
latLonList.add(LatLon.fromRadians(0.0, 180.0));
latLonList.add(LatLon.fromRadians(0.0, -90.0));
latLonList.add(LatLon.fromRadians(0.0, -180.0));
After we have a list of LaLon classes we can instantiate the SurfacePolyline class. The constructor accepts the color and stroke color.
// Creating a SurfacePolyline out of our points
SurfacePolyline surfacePolyline1 = new SurfacePolyline(latLonList, Color.CYAN, Color.WHITE);
surfacePolyline1.setAntiAlias(true);
The other type of polyline is the Polyline class. This class is slightly more useful because it accepts a list of Position classes. Position classes are very similar to the LatLon class however it accepts a third parameter for elevation.
//Example of Positions for use with the Polyline class.
List<Position> positionList = new ArrayList<Position>();
positions.add(new Position(Angle.fromRadians(36.0403), Angle.fromRadians(-93.34067), 309.0));
positions.add(new Position(Angle.fromRadians(36.0403), Angle.fromRadians(-93.34067), 309.0));
positions.add(new Position(Angle.fromRadians(36.04019), Angle.fromRadians(-93.34067), 310.0));
positions.add(new Position(Angle.fromRadians(36.04012), Angle.fromRadians(-93.34098), 312.0));
positions.add(new Position(Angle.fromRadians(36.04004), Angle.fromRadians(-93.34106), 316.0));
After we form our polyline we can create a new layer and add it to the layer list.
// Creating a new layer
RenderableLayer polylineLayer = new RenderableLayer();
polylineLayer.addRenderable(surfacePolyline1);
// Add layer to the layer list
LayerList layers = m.getLayers(); //this.worldWindowGLCanvas1.getModel().getLayers();
layers.add(polylineLayer);
Alright, so that is a very basic intriduction to the Polylines in World Wind. If you where following along in the Hello World Wind Article, you can just copy and paste the code below into HelloWorldWindMain() below initComponents();.
Model m = (Model) WorldWind.createConfigurationComponent(AVKey.MODEL_CLASS_NAME);
this.worldWindowGLCanvas1.setModel(m);
// Equator line
List<LatLon> latLonList = new ArrayList<LatLon>();
latLonList.add(LatLon.fromRadians(0.0, -180.0));
latLonList.add(LatLon.fromRadians(0.0, 90.0));
latLonList.add(LatLon.fromRadians(0.0, 180.0));
latLonList.add(LatLon.fromRadians(0.0, -180.0));
// Creating a SurfacePolyline out of our points
SurfacePolyline surfacePolyline = new SurfacePolyline(latLonList1, Color.CYAN, Color.WHITE);
surfacePolyline.setAntiAlias(true);
// Creating a new layer
RenderableLayer polylineLayer = new RenderableLayer();
polylineLayer.addRenderable(surfacePolyline);
// Add layer to the layer list
LayerList layers = m.getLayers();
layers.add(polylineLayer);
Run the project (F6) and see the SurfacePolyline follow the equator.


Just what the doctor ordered, very helpful. Question other than the actual code it’self where can I find some documentation on the java World Wind?
September 21, 2007 at 7:40 pm
Documentation is pretty hard to find, which is the reason for these articles. Mainly I have been using the World Wind forum, the Wiki Site and the World Wind docs packed with the source code. Besides that it’s been trial and error unfortunately.
Forums – http://forum.worldwindcentral.com/forumdisplay.php?f=37
Wiki – http://www.worldwindcentral.com/wiki/Java
September 24, 2007 at 8:44 am
I am a newbie to WWJ. I tried the above code and it doesnt seem to work. I am not sure if I have to include a specific header. I get errors indicating unrecognized symbols and variables. Please help!
Thanks,
ts
February 23, 2008 at 12:09 pm
Compiles – but no result. I get errors …
private void initComponents() {
[...]
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(worldWindowGLCanvas1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
);
[...]
Error:
setHorizontalGroup(javax.swing.GroupLayout.Group) in javax.swing.GroupLayout cannot be applied to (javax.swing.GroupLayout.ParallelGroup.addComponent)
cannot find symbol
symbol : method addComponent(gov.nasa.worldwind.awt.WorldWindowGLCanvas,int,int,short)
location: class javax.swing.GroupLayout.ParallelGroup
… the same goes for the vertical group. Any suggestions ?
October 19, 2008 at 5:45 pm
Hey man this Code isn’t working for me… I copied and pasted it as you said but I keep getting errors on these two lines
SurfacePolyline surfacePolyline = new SurfacePolyline(latLonList, Color.CYAN, Color.WHITE);
surfacePolyline.setAntiAlias(true);
I Fixed all the imports and that fixed everything else. but it keeps saying.
cannot find symbol
symbol: constructor SurfacePolyline(java.util.List,java.awt.Color,java.awt.Color)
location: class gov.nasa.worldwind.render.SurfacePolyline
I think this error is referring to that there is no class that accepts a List and Two Color parameters…
cannot find symbol
symbol: method setAntiAlias(boolean)
location: class gov.nasa.worldwind.render.SurfacePolyline
Thanks
June 11, 2009 at 10:38 am
*** I had this annoying problem too and google’d high and low to no avail. I am a novice at this netbean/java stuff. (Im a Fortran77 man myself)
But after trial and error, I found it was fixed by adding all 3 JARs to the “Project properties”, “Libraries” to BOTH the “Compile time” and “Run time” library paths.
i.e. Browsing to the Worldwind toplevel directory and selecting worldwind.jar, gluegen-rt.jar & jogl.jar
Not sure why this works, but I now have a some working demos
October 14, 2010 at 9:13 am