JMapViewer to make a MapMarkerDot "clickable"?

0

I am using JMapViewer , what I want to do is:

  • In the JMapViewer when clicking on the world map, add a MapMarkerDot just where the click was made.

  • By clicking or passing the pointer on the MapMarkerDot that have been added, it is possible to visualize the latitude and longitude of each of them, the general idea is that each MapMarkerDot is "clickable".

I can not find a way to make a 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);

    }
}
    
asked by Bryan Romero 23.10.2016 в 17:47
source

1 answer

1

Probably you should use JXMapViewer2, which has a better design.

Anyway, the solution for what you plan with JMapViewer is to do hit testing with the markers. You can do something like the following:

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

}

To register it with the map,

theMap = new JMapViewer();        

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

A dirty example on how to use it would be something like,

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

    }

}
    
answered by 17.11.2016 / 02:17
source