6

单击地球时,我在获取位置(纬度/经度)时遇到问题。

SO(和其他网站)上的任何地方都建议使用getCurrentPosition方法。

不幸的是,这会返回包含点击点的顶部可拾取对象的位置,因此如果那里不存在可拾取对象,该方法只会返回null

当您使用任何示例时,您可以在进入状态栏时看到它:即使出于这个原因鼠标在地球上,也会时不时出现Off Globe标签(而不是纬度/经度)!

有没有其他方法可以在不依赖可拾取对象的情况下获得位置?我正在考虑通过屏幕上的位置和使用几何来计算,但这会非常困难,我不知道从哪里开始......

4

1 回答 1

2

我不确定getCurrentPosition()你指的是哪个,但WorldWindow#getCurrentPosition()应该做你想做的事。javadocs说:

返回当前光标位置的当前纬度、经度和高度,如果光标不在地球上,则返回 null。

如果您的光标不与地球相交(即您单击背景中的星星),则不会有与单击相关联的位置。这不依赖于可拾取的对象,只依赖于在单击时与光标相交的地球。

以下示例适用于我:

public class PositionListener implements MouseListener {
    private final WorldWindow ww;

    public PositionListener(WorldWindow ww) {
        this.ww = ww;
    }
    @Override
    public void mouseClicked(MouseEvent event) {
        try {
            System.out.println(ww.getCurrentPosition().toString());
        } catch (NullPointerException e) {
            // click was not on the globe
        }
    }
    //...
}

如果getCurrentPosition()不适合您,这是另一种选择:

@Override
public void mouseClicked(MouseEvent event) {
    Point p = event.getPoint();
    Vec4 screenCoords = new Vec4(p.x,p.y);
    Vec4 cartesian = ww.getView().unProject(screenCoords);
    Globe g=ww.getView().getGlobe();
    Position pos=g.computePositionFromPoint(cartesian);
    System.out.println(pos.toString());
}
于 2016-02-16T15:51:38.153 回答