Friday, February 8, 2008

How to get coordinates from the map

Since we are talking about querying a map, let's quickly see the important part of the code behind:

1) the MapMouseListener has to be implemented by your tool
2) the class implementing it has to be added as listener to the mapdisplay:


IMap map = ApplicationGIS.getActiveMap();
((ViewportPane) map.getRenderManager().getMapDisplay()).addMouseMotionListener(implementingClass);


3) your implementing class will have some nice method, one of which should look like:



public void mouseReleased( MapMouseEvent event ) {
IMap map = ApplicationGIS.getActiveMap();
if (map != null) {
// event.x, event.y gives the pixel position of the mouse in the map
// next ask for the world coordinates of the clicked point
Coordinate coord = map.getViewportModel().pixelToWorld(event.x, event.y);
// and here the pixel position of the mouse on the screen
Point mouse = getDisplay().getCursorLocation();

// do something with it :)
}
}
Post a Comment