This discussion is archived
2 Replies Latest reply: Apr 30, 2013 7:47 PM by Maxideon RSS

how to highlight result set data on world map

1003916 Newbie
Currently Being Moderated
Hi,
I want to show the graphical representation of world map to highlight the countries which having the maximum booking of hotels of our client xyz.

All the values are coming from the mysql db .

I want the library which will draw the world map on the Jpanel for the respective result set

Plz help me for this .

I generally use the JFreeChart for drawing the other graphs but the Jfreechart don't have the library for the World Map or Destination Graph etc.

Hint: ( Google Analytic's map is the best example )

Thanks In Advanced

Mahesh
  • 1. Re: how to highlight result set data on world map
    jduprez Pro
    Currently Being Moderated
    Hello,
    I assume you have searched for Java libraries representing maps and have not found a suitable one.

    If you're going to develop this yourself, I can think of a dense way:
    <ol>
    <li> find a set of vectorial maps, where you can extract each country's borders (I have found vectorial maps on the Web, but I'm not sure if you can easily extract each country)
    <li> for each country, build a Polygon out of the vector data.
    <li> use built-in method Graphics.fillPolygon(...)
    </ol>

    Extraction in Point 1 is probably the most difficult or tedious part of the job, unless you find an existing set of maps already structured per country.

    Best regards,

    J.
  • 2. Re: how to highlight result set data on world map
    Maxideon Explorer
    Currently Being Moderated
    This isn't what you want, but it will get you on the right track. I chose the most complex image I could find so give it a few seconds to read the image and build the paths. The parser is catered to the image I chose, but in general you can probably expect the "path" element to delineate the boundaries for any map svg. The particular image I chose grouped the paths with country ID so I added a little mouse interaction to the GUI. Forgive me for any dumbly coded stuff. I didn't put much effort into streamlining it.
    import org.xml.sax.*;
    import java.awt.*;
    import javax.swing.*;
    import javax.xml.parsers.SAXParser;
    import javax.xml.parsers.SAXParserFactory;
    import org.xml.sax.helpers.DefaultHandler;
    import java.awt.geom.Path2D;
    import java.util.StringTokenizer;
    import java.net.URL;
    
    public class WorldMapTest {
        public static void main(String[] args) throws Exception {
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser saxParser = factory.newSAXParser();
    
            SimpletonPathExtractor handler = new SimpletonPathExtractor();
            saxParser.parse(new URL("http://upload.wikimedia.org/wikipedia/commons/b/b7/World98.svg").openStream(), handler);
            
            createAndShowGUI(handler.locations);
        }
        public static void createAndShowGUI(final java.util.List<Location> locations) {
            JFrame frame = new JFrame();
            JPanel p = new JPanel() {
                @Override
                public Color getBackground() {
                    return Color.white;
                }
                public void paintComponent(java.awt.Graphics g) {
                    super.paintComponent(g);
                    
                    Graphics2D g2 = (Graphics2D) g;
                    g2.scale(getWidth() / 8000.0, getHeight() / 3859.0);
                    g2.setColor(Color.black);
                    
                    Point p = getMousePosition();
                    if(p != null) {
                        p.x = (int) (p.x * 8000/getWidth());
                        p.y = (int) (p.y * 3859/getHeight());
                    }
                    
                    String placeID = null;
                    for (Location loc : locations) {
                        if(p != null && placeID == null && loc.boundry.contains(p)){
                            g2.setColor(Color.LIGHT_GRAY);
                            g2.fill(loc.boundry);
                            placeID = loc.id == null?"Unkown":loc.id;
                            g2.setColor(Color.black);
                        }
                        g2.draw(loc.boundry);
                    }
                    
                    if (placeID != null) {
                        g2.setColor(Color.green.darker());
                        Font f = g2.getFont();
                        f = f.deriveFont(Font.BOLD,(f.getSize() * 8000f / getWidth()));
                        g2.setFont(f);
                        g2.drawString(placeID, p.x, p.y);
                    }
                }
                public java.awt.Dimension getPreferredSize() {
                    return new Dimension(1000,482);
                }
            };
            p.addMouseMotionListener(new java.awt.event.MouseAdapter() {
                @Override
                public void mouseMoved(java.awt.event.MouseEvent e) {
                    ((JComponent) e.getSource()).repaint();
                }
            });
    
            frame.setContentPane(new JScrollPane(p));
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();
            frame.setVisible(true);
        }
        public static class SimpletonPathExtractor extends DefaultHandler {
            java.util.List<Location> locations = new java.util.ArrayList<>();
            
            String recentID;
            @Override
            public void startElement(String uri, String localName, String qName, Attributes attr) throws SAXException {
                if("g".equals(qName)) {
                    recentID = attr.getValue("id");
                }
                if ("path".equals(qName)) {
                    int length = attr.getLength();
                    for (int i = 0; i < length; i++) {
                        String attrN = attr.getQName(i);
                        String val = attr.getValue(attrN);
                        if ("d".equals(attrN)) { //path data
                            Path2D path = new Path2D.Double();
    
                            //assume simple polygonal path
                            StringTokenizer tk = new StringTokenizer(val, "MLzZ ");
    
                            try {
                                path.moveTo(Double.parseDouble(tk.nextToken()),
                                            Double.parseDouble(tk.nextToken()));
                                while (tk.hasMoreTokens()) {
                                    path.lineTo(Double.parseDouble(tk.nextToken()),
                                                Double.parseDouble(tk.nextToken()));
                                }
                                path.closePath();
                            } catch (Exception e) {
                                throw new Error("I'm a simple parser. I can only do "
                                        + "'LineTo' paths!", e);
                            }
                            locations.add(new Location(path,recentID));;
                        }
                    }
                }
            }
            public void endElement(String uri, String localName, String qName) throws SAXException {
                if("g".equals(qName)) {
                    recentID = null;
                }
            }
        }
        public static class Location {
            Path2D boundry;
            String id;
            
            public Location(Path2D boundry, String id) {
                this.boundry = boundry; this.id = id;
            }
        }
    }

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points