3


I am trying to insert a Map View in a Java Swing application for the user to visualize and edit his recorded .gpx routes.
I saw in this thread people recommending the MapPanel API, however I can't find any documentation. I can insert the map on my app, however I need more documentation to understand the API functionalities. Anyone can help me on this matter? This is what I have so far:

MapPanel mapPanel = new MapPanel();
mapPanel.setBounds(276, 77, 722, 632);
frame.add(mapPanel);

Problems:
1) I can't disable the information window that is shown in front of the map

mapPanel.getOverlayPanel().setVisible(!mapPanel.getOverlayPanel().isVisible()); //disable the overlay info box
mapPanel.getControlPanel().setVisible(!mapPanel.getControlPanel().isVisible()); //disable the overlay control box

2) Can I draw routes on top of the map?
3) Can I insert waypoints on the map?

Thanks for your help ;)
Filipe

Community
  • 1
  • 1
ffleandro
  • 4,039
  • 4
  • 33
  • 48

2 Answers2

4

1) Looking at the source code that they post, I'd emulate what they do when creating my MapPanel GUI, something like this

import java.awt.Dimension;
import javax.swing.*;
import com.roots.map.MapPanel.Gui;

public class TestMapPanel {
   public static void main(String[] args) {

      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            Gui mapPanel = new Gui();
            mapPanel.setPreferredSize(new Dimension(722, 632));

            JMenuBar menuBar = mapPanel.createMenuBar();


            JFrame frame = new JFrame("Map Panel Test");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().add(mapPanel);
            frame.setJMenuBar(menuBar);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
         }
      });      
   }
}

Then the user can select if they want to view the info panel or not by simply checking the JCheckBoxMenuItem that corresponds to the info panel.

2) and 3) Anything is possible, but you'll need to study the source code to see how to best do these and other things.

Edit, to get rid of the search panel on start up, I've resorted to the kludge:

        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        SwingUtilities.invokeLater(new Runnable() {
           public void run() {
              for (int i = 0; i < menuBar.getMenuCount(); i++) {
                 JMenu menu = menuBar.getMenu(i);
                 if ("View".equals(menu.getText())) {
                    int componentCount = menu.getMenuComponentCount();
                    for (int j = 0; j < componentCount; j++) {
                       Component c = menu.getMenuComponent(j);
                       if (c instanceof JCheckBoxMenuItem) {
                          JCheckBoxMenuItem chkBoxMenuItem = (JCheckBoxMenuItem) c;
                          String text = chkBoxMenuItem.getText();
                          if ("Show SearchPanel".equals(text)) {
                             chkBoxMenuItem.doClick();
                          }
                       }
                    }
                 }
              }
           }
        });

It shouldn't have to be this clunky, and if I'm reading and interpreting the source code correctly, than my opinion is that the source could be better.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • First of all, thanks for your reply. I gave a look on the sourcecode and solved the 1) issue. I think you overkilled a little bit. My solution was: mapPanel.getOverlayPanel().setVisible(!mapPanel.getOverlayPanel().isVisible()); //disable the overlay information box mapPanel.getControlPanel().setVisible(!mapPanel.getControlPanel().isVisible()); //disable the overlay control box – ffleandro Sep 10 '11 at 19:58
  • Ok, so 1) solved (Edited the solution on the original post). How about the 2) and 3) problems? In the sourcecode I don't think I see these functionalities implemented. I'm thinking about implementing them myself, however I don't see how i can fully do it. For instance if I had a static image of a certain region of the map, I could draw a route on top of it. However if the user spans or zooms I'll have to adapt my drawings to the new visible region. How can I do this? – ffleandro Sep 10 '11 at 20:09
  • Regarding your first comment: but my solution gives you the JMenuBar which is likely useful. As for 2) and 3), I think you're going to have to do some brute-force coding to implement this functionality. – Hovercraft Full Of Eels Sep 10 '11 at 20:52
0

Although it is not the answer for my question, I used the the JXMapViewer from the Swingx API to implement what I wanted. Thanks for your answers though.

ffleandro
  • 4,039
  • 4
  • 33
  • 48