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 :)
}
}

2 comments:

Anonymous said...

Get the coordinates of any place in the World
Put coordinates to a place in the World. For example, if you're looking for a place in London, find it by entering the name of a nearby street in a form. Then move around the map to the place you're looking for. Each time you click on the map will show its coordinates.

moovida said...

Thanks man, I like that site. It is really usefull whenever you are dealing with engines that only work with lat/long. Usefull.