Exact position by mouse click in WorldWind
Asked Answered
G

1

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

Gilges answered 29/1, 2016 at 15:58 Comment(2)
Check this post: #20160920Inessa
as I said above aCanvas.getCurrentPosition() returns null because is based on pickablesGilges
H
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());
}
Hensel answered 16/2, 2016 at 15:51 Comment(9)
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.Gilges
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?Hensel
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 moreGilges
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.Hensel
I added an alternative method that does not rely on getCurrentPosition()Hensel
That is interesting. Sounds like what I was actually looking for. I will try it later.Gilges
Your second solution still doesn't work, but it is much better than using getCurrentPosition. I will try to improve it.Gilges
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.Hensel
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 sorryGilges

© 2022 - 2024 — McMap. All rights reserved.