我有一个 WorldWindow
,上面有各种 RenderableLayer
。我想在运行时添加一个 CompassLayer
。
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(); }
不幸的是,我在屏幕上的任何地方都没有看到指南针。我尝试过:
- 使用
compass.setLocationCenter(...) 手动设置罗盘位置
- 添加另一种类型的图层(带有
UserFacingIcon
的IconLayer
),效果很好,表明这不是我添加图层或类似内容的固有问题。 记录当前图层以确保其已添加,使用:
logger.debug("当前图层 = "+ worldWindow.getModel().getLayers().toString());
我回来了:
Cur layers = Stars, Atmosphere, Bing Imagery VASCustom, Scale bar, Compass, View Controls, Renderable, Renderable, Renderable, Renderable, Compass,
如何有效地调试我的隐形罗盘问题?谢谢!
更新
我已经简化了我的代码以使用现有 compassLayer
,并且我确定问题是我对 setLocationCenter
的使用,即,
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);
所以我需要确定 setLocationCenter
逻辑到底出了什么问题。
最佳答案
我的代码有两个主要问题。
我已经有一个
compassLayer
并且应该使用它而不是制作一个新的,即compass = (CompassLayer) worldWindow.getModel().getLayers().getLayerByName("Compass");
当您使用
computePointFromPosition()
时你会得到绝对笛卡尔坐标。然后您需要使用View.project()
将它们转换为像素坐标。最后你需要抵消当前的View
。即,
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));
修复这些问题解决了问题,现在指南针出现了。
关于java - CompassLayer 未显示在 WorldWind 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60513641/