加载静态谷歌地图

时间:2013-11-23 23:19:19

标签: java swing google-maps

我尝试在以下代码行中加载静态Google地图。

The request to connect to the specified URL has failed with java.net.ConnectException: Connection timed out: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.net.NetworkClient.doConnect(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.<init>(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
at StaticGoogleMap.init(StaticGoogleMap.java:36)
at StaticGoogleMap.main(StaticGoogleMap.java:66)

代码:

  public class StaticGoogleMap extends JPanel
    {
        private JLabel label;
        private JFrame frame;
        private ImageIcon icon;
        private URLConnection con;
        private URL url;




        public void init()
        {
            try 
            {
            url=new URL("http://maps.google.com/maps/api/staticmap?center=New+York,NY&zoom=14&size=512x512&maptype=roadmap&sensor=false");
            con = url.openConnection();
            con.connect();
            InputStream is = con.getInputStream();
            BufferedImage img=ImageIO.read(is);
            icon=new ImageIcon(img);
            label=new JLabel(icon);
            add(label);
            frame=new JFrame("Google Map");
            frame.add(this,java.awt.BorderLayout.CENTER);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLocationRelativeTo(null);
            frame.setSize(500,500);
            frame.setVisible(true);
            is.close();

            } catch (MalformedURLException e) 
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) 
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
        public static void main(String[] args)
        {
            new StaticGoogleMap().init();
        }

    }

0 个答案:

没有答案