Thursday, December 26, 2013

Adding Google Maps to your Java Standalone applications

Google maps is a very impressive tool to present details of particular location in any part of world. It is very common to see google maps integration in web applications using java scripts. However it can be included in to Java SE applications also. It can be simply done using google static maps api and java swing components. Here is my code.

public class Main {
     JFrame frame = new JFrame();
     JPanel panel;
     BufferedImage image;
     public void show(String gps) {
            panel = new JPanel();
            try {
                   image = ImageIO.read(new URL("http://maps.google.com/staticmapcenter="+gps+"&zoom=14&size=600x300&maptype=roadmap&markers="+gps+"&sensor=false&key=ABQIAAAAgb5KEVTm54vkPcAkU9xOvBR30EG5jFWfUzfYJTWEkWk2p04CHxTGDNV791-cU95kOnweeZ0SsURYSA&format=jpg"));

                   JLabel label = new JLabel(new ImageIcon(image));
                   panel.add(label);
                   frame.add(panel);
                   frame.pack();
                   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                   frame.setLocationRelativeTo(null);
                   frame.setVisible(true);
            } catch (MalformedURLException e) {
                   e.printStackTrace();
            } catch (Exception e) {
                  e.printStackTrace();
            }
     }
     public Main(){
             show("6.423601,79.996755");
     }

     public static void main(String[] args){
           new Main();
     }

}





More details can be added to map by referring to google static maps api.