0

I'm displaying an OSM map with the library jxmapviewer2.

I cannot manage to get the lat lon of a point of the map, for istance I would like to get the geographic coordinates of a spot by clicking it

public void mouseClicked(MouseEvent e) {
  if(e.getClickCount() == 1 && e.getButton() == MouseEvent.BUTTON3){
    java.awt.Point p = e.getPoint();
    double X = p.getX();
    double Y = p.getY();
    System.out.println("X:"+X+",Y:"+Y);
  }
}

This code above returns an output like:

X:239.0,Y:113.0
mortalis
  • 2,060
  • 24
  • 34
ro.nin
  • 121
  • 5
  • 13
  • 1
    What component have you added the mouse listener too? Most likely you have to use `public GeoPosition convertPointToGeoPosition(Point2D pt)` that is part of the class `JXMapViewer`. – matt May 30 '17 at 11:08
  • It worked, Thank you! – ro.nin May 30 '17 at 11:33
  • You should consider marking the actual answer as correct then, if that is what you did. – matt May 30 '17 at 11:45

1 Answers1

2

Assuming you have created a JXMapViewer and added your mouse listener to that, then you can use your viewer to get the GeoPosition.

JXMapViewer viewer;
//initialize somehow.
viewer.addMouseListener(new MouseAdapter(){
     public void mouseClicked(MouseEvent e) {
            if(e.getClickCount() == 1 && e.getButton() == MouseEvent.BUTTON3){
                java.awt.Point p = e.getPoint();
                GeoPosition geo = viewer.convertPointToGeoPosition(p);
                System.out.println("X:"+geo.getLattitude()+",Y:"+geo.getLongitude());
            }
     }
});
matt
  • 10,892
  • 3
  • 22
  • 34