6

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...

Alessandro Ruffolo
  • 1,575
  • 1
  • 16
  • 34

1 Answers1

2

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());
}
Chris K
  • 1,703
  • 1
  • 14
  • 26
  • That is what javadocs *says*. But if you take a look to the implementation of getCurrentPosition (inside WorldWindowImpl) you will see it actually uses PickedObjectList. – Alessandro Ruffolo Feb 16 '16 at 16:20
  • 1
    I am able to click in an area on the globe where I have not added any objects and get a position back, can you give a screenshot of where you click and do not get a position back? – Chris K Feb 16 '16 at 16:23
  • also the maps are pickables (terrainobjects), thus it gives you back a position even if there are not objects. Problem is, it gives you back the point where that map has one of its corners. I don't have the application right now, later I can post something more – Alessandro Ruffolo Feb 16 '16 at 16:34
  • 1
    I can update the position on mouse move and I see it change every time I move the cursor - it does not appear to be using corners. – Chris K Feb 16 '16 at 16:47
  • I added an alternative method that does not rely on `getCurrentPosition()` – Chris K Feb 16 '16 at 17:10
  • That is interesting. Sounds like what I was actually looking for. I will try it later. – Alessandro Ruffolo Feb 16 '16 at 17:16
  • Your second solution still doesn't work, but it is much better than using getCurrentPosition. I will try to improve it. – Alessandro Ruffolo Feb 16 '16 at 23:09
  • Are you zoomed very far in and angled tangent to the surface of the earth? That was the only case I was able to get null results from `getCurrentPosition()` - I wouldn't expect either method to work particularly well in that case because what is rendered on the screen probably does not align exactly with the model word wind is using to compute coordinates. – Chris K Feb 17 '16 at 00:03
  • i was perpendicular to the surface. about the distance sometimes close, sometimes far. But I have to say that the closer I go, the better are the positions with the calculated method. PS this for me is a side project, thus I am not able to work on it full-time and I cannot give you feedback often, I'm sorry – Alessandro Ruffolo Feb 18 '16 at 09:09