Get latitude longitude from a jxmapviewer2

746 views Asked by At

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
1

There are 1 answers

0
matt On BEST ANSWER

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());
            }
     }
});