0

Estoy usando JMapViewer , lo que quiero hacer es:

  • En el JMapViewer al hacer click sobre el mapa mundo se añada un MapMarkerDot justo donde se realizo el click.

  • Al hacer click o pasar el puntador sobre lo MapMarkerDot que han sido añadidos , se logre visualizar la latitud y longitud de cada uno de ellos, la idea general es que cada MapMarkerDot sea "clickable".

No logro encontrar la forma de hacer un MapMarkerDot "clickable".

import java.awt.EventQueue;
import java.awt.Point;

import javax.swing.JFrame;
import javax.swing.JPanel;

import org.openstreetmap.gui.jmapviewer.JMapViewer;
import org.openstreetmap.gui.jmapviewer.MapMarkerDot;
import org.openstreetmap.gui.jmapviewer.interfaces.ICoordinate;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JLabel;

public class Map {

    private JFrame frame;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Map window = new Map();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public Map() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 547, 458);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        JMapViewer theMap = new JMapViewer();
        theMap.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {

                Point point = e.getPoint();

                ICoordinate coord = theMap.getPosition(point);

                theMap.addMapMarker(new MapMarkerDot(coord.getLat(), coord.getLon()));

            }
        });
        theMap.setBounds(0, 0, 487, 263);

        JPanel panel = new JPanel();
        panel.setBounds(21, 25, 487, 263);
        frame.getContentPane().add(panel);
        panel.setLayout(null);

        panel.add(theMap);

        JLabel numeroMarkerDot = new JLabel("Numero");
        numeroMarkerDot.setBounds(21, 323, 67, 14);
        frame.getContentPane().add(numeroMarkerDot);

        JLabel Latitud = new JLabel("markerLatitud");
        Latitud.setBounds(93, 323, 91, 14);
        frame.getContentPane().add(Latitud);

        JLabel Longitud = new JLabel("markerLongitud");
        Longitud.setBounds(222, 323, 113, 14);
        frame.getContentPane().add(Longitud);

    }
}

1 respuesta 1

1

Probablemente te conviene usar JXMapViewer2, que tiene un mejor diseño.

De todas formas, la solución para lo que planteas con JMapViewer pasa por hacer hit testing con los marcadores. Puedes hacer algo como lo siguiente:

import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import org.openstreetmap.gui.jmapviewer.JMapViewer;
import org.openstreetmap.gui.jmapviewer.interfaces.MapMarker;

public class CurrentMarkerAdapter extends MouseAdapter {
    
    private JMapViewer map;
    private MapMarker currentMarker;
    
    public CurrentMarkerAdapter(JMapViewer map) {
        this.map = map;
        currentMarker = null;
    }
    
    public MapMarker getCurrentMarker() {
        return currentMarker;
    }   
    
    public void setCurrentMarker(MapMarker marker) {
        currentMarker = marker;
    }
    
    @Override
    public void mouseClicked(MouseEvent e) {
        Point mousePoint = e.getPoint();
        currentMarker = null;
        for (MapMarker marker : map.getMapMarkerList()) {
            Point p = map.getMapPosition(marker.getLat(), marker.getLon(), true);
            if (p != null) {
                int r = map.getRadius(marker, p);
                Rectangle rect = new Rectangle(p.x - r, p.y - r, r + r, r + r);
                if (rect.contains(mousePoint)) {
                    currentMarker = marker;
                    break;
                }
            }           
        }
    }
    
}

Para registrarlo con el mapa,

theMap = new JMapViewer();        

CurrentMarkerAdapter cma = new CurrentMarkerAdapter(theMap);
theMap.addMouseListener(cma);

Un ejemplo sucio sobre cómo usarlo sería algo como,

theMap.addMouseListener(new MouseAdapter() {
    
    @Override
    public void mouseClicked(MouseEvent e) {

        // Todos en azul
        for (MapMarker marker : theMap.getMapMarkerList()) {
            if (marker instanceof MapMarkerDot) {
                ((MapMarkerDot)marker).setBackColor(Color.BLUE);
            }
        }
        
        MapMarker current = cma.getCurrentMarker();
        
        // Si no hay actual, añadimos marcador nuevo y lo hacemos actual
        if (current == null) {
            Point point = e.getPoint();
            ICoordinate coord = theMap.getPosition(point);
            current = new MapMarkerDot(coord.getLat(), coord.getLon());
            theMap.addMapMarker(current);
            cma.setCurrentMarker(current);
        }

        // Coloreamos el actual
        if (current instanceof MapMarkerDot) {
            ((MapMarkerDot)current).setBackColor(Color.RED);
        }
        
        theMap.repaint();
        
    }
    
}
1
  • Muchas gracias! me sirvio , llevaba semanas con este problema. el 22 nov. 2016 a las 22:57

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.