I have a WorldWindow
with various RenderableLayer
s 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:
compass.setLocationCenter(...)
IconLayer
with a UserFacingIcon
), 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!
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 compassLayer
and 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 using View.project()
. Finally you need to offset by the current View
. Ie,
Vec4 vecOwnship = worldWindow.getModel()
.getGlobe()
.computePointFromPosition(ownshipPosition);
Vec4 vecScreen = worldWindow.getView().project(vecOwnship);
Rectangle viewPort = worldWindow.getView().getViewport();
compass.setLocationCenter(new Vec4(viewPort.x + vecScreen.x, viewPort.y + vecScreen.y, 0));
Fixing these issues solved the problem and now the compass shows up.