2 Replies Latest reply on May 1, 2013 2:47 AM by Maxideon

    how to highlight result set data on world map

      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

        • 1. Re: how to highlight result set data on world map
          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:
          <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(...)

          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,

          • 2. Re: how to highlight result set data on world map
            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);
                public static void createAndShowGUI(final java.util.List<Location> locations) {
                    JFrame frame = new JFrame();
                    JPanel p = new JPanel() {
                        public Color getBackground() {
                            return Color.white;
                        public void paintComponent(java.awt.Graphics g) {
                            Graphics2D g2 = (Graphics2D) g;
                            g2.scale(getWidth() / 8000.0, getHeight() / 3859.0);
                            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)){
                                    placeID = loc.id == null?"Unkown":loc.id;
                            if (placeID != null) {
                                Font f = g2.getFont();
                                f = f.deriveFont(Font.BOLD,(f.getSize() * 8000f / getWidth()));
                                g2.drawString(placeID, p.x, p.y);
                        public java.awt.Dimension getPreferredSize() {
                            return new Dimension(1000,482);
                    p.addMouseMotionListener(new java.awt.event.MouseAdapter() {
                        public void mouseMoved(java.awt.event.MouseEvent e) {
                            ((JComponent) e.getSource()).repaint();
                    frame.setContentPane(new JScrollPane(p));
                public static class SimpletonPathExtractor extends DefaultHandler {
                    java.util.List<Location> locations = new java.util.ArrayList<>();
                    String recentID;
                    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 {
                                        while (tk.hasMoreTokens()) {
                                    } 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;