I have a WorldWindow with various RenderableLayers on it. I'd like to add a CompassLayer at run-time.
try {
String compassPath = "images" + File.separator + "CompassRoseWhite.png";
String compassImg = new ClassPathResource(compassPath).getURL().toString();
compass = new CompassLayer(compassImg);
worldWindow.getModel().getLayers().add(compass);
} catch (IOException e) { e.printStackTrace(); }
Unfortunately, I do not see the compass anywhere on-screen. I have tried:
- manually setting the compass position using
compass.setLocationCenter(...) - adding another type of layer (an
IconLayerwith aUserFacingIcon), and this worked fine, indicating it's not an intrinsic issue with how I am adding layers or anything like that. logging the current layers to make sure it was added, using:
logger.debug("Cur layers = " + worldWindow.getModel().getLayers().toString());
I got back:
Cur layers = Stars, Atmosphere, Bing Imagery VASCustom, Scale bar, Compass, View Controls, Renderable, Renderable, Renderable, Renderable, Compass,
How can I effectively debug my invisible compass problem? Thank you!
Update
I've simplified my code to use the existing compassLayer, and I've determined the problem is my use of setLocationCenter, i.e.,
compass = (CompassLayer) worldWindow.getModel().getLayers().getLayerByName("Compass");
// this works ...
// compass.setPosition(AVKey.SOUTHEAST);
// compass.setLocationOffset(new Vec4(0, 20));
// this does not work ...
compass.setLocationCenter(worldWindow.getView().getCenterPoint());
// this part works fine
String compassPath = "images" + File.separator + "CompassRoseWhite.png";
String compassImg = new ClassPathResource(compassPath).getURL().toString();
compass.setIconFilePath(compassImg);
compass.setEnabled(true);
So what I need to determine is what exactly is wrong with the setLocationCenter logic.
There were two primary problems with my code.
I already had a
compassLayerand should have used it instead of making a new one, ie,compass = (CompassLayer) worldWindow.getModel().getLayers().getLayerByName("Compass");When you use
computePointFromPosition()you get back absolute Cartesian coordinates. Then you need to convert these to pixel coordinates usingView.project(). Finally you need to offset by the currentView. Ie,Fixing these issues solved the problem and now the compass shows up.