I am having problems into getting the position (lat/lon) when I click on the globe.
Everywhere on SO (and other websites) suggests to use the getCurrentPosition method.
Unfortunately, this returns the position of the top pickable object which comprehend the point clicked, thus if no pickable object is present there, the method just returns null
You can see it when into the status bar when you use any example: there is every now and then the Off Globe label (instead of lat/lon) even if the mouse is in the globe for this very reason!
Is there any other way to obtain the position without depending on pickable objects? I was thinking about calculating through position on the screen and using geometry, but this would be very much hard and I wouldn't know where to begin...
I'm not sure to which getCurrentPosition()
you are referring, but WorldWindow#getCurrentPosition()
should do what you want. The javadocs say:
Returns the current latitude, longitude and altitude of the current cursor position, or null if the cursor is not on the globe.
If your cursor does not intersect with the globe (i.e. you click on the stars in the background), there is not going to be a position associated with the click. This does not rely on pickable objects, just on the globe intersecting the cursor at the time of the click.
The following example works for me:
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
}
}
//...
}
If getCurrentPosition()
is not working for you this is an alternative:
@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());
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With