【问题标题】:How to access google maps api in Java Application?如何在 Java 应用程序中访问谷歌地图 API?
【发布时间】:2010-01-03 04:22:52
【问题描述】:

如何从 Java 应用程序访问 google maps API?

【问题讨论】:

    标签: java google-maps


    【解决方案1】:

    您可以使用Swing-WS,一个组件 JXMapViewer 可用,并提供与 JavaScript 版本类似的功能。但是,在提供的 API 之外访问 Google tile 服务器仍然是不合法的:JavaScript 和 Flash。

    跟踪此请求存在一个问题:http://code.google.com/p/gmaps-api-issues/issues/detail?id=1396。它已获得批准,但谁知道它何时可用。

    【讨论】:

      【解决方案2】:

      对于客户端 Java,您最好的选择是 Static Maps API。对于服务器端 Java,答案在很大程度上取决于您用于开发的框架。话虽如此,Google Maps API is well documented.

      【讨论】:

        【解决方案3】:

        您可以使用 Swing Labs,来自 swingx 的 JXMapKit: http://today.java.net/pub/a/today/2007/10/30/building-maps-into-swing-app-with-jxmapviewer.html

        这很简单。欲了解更多信息,请参阅网站。

        JXMapKit mapView = new JXMapKit();
        mapView.setDefaultProvider(DefaultProviders.OpenStreetMaps);
        mapView.setDataProviderCreditShown(true);
        add(mapView)
        

        看起来像这样:


        (来源:java.net

        看一下上面文章中的源码,三行代码,就可以轻松查看地图:

        【讨论】:

          【解决方案4】:

          如果您只是在寻找静态地图,您可以使用此代码来使地图正常工作:

          import java.awt.BorderLayout;
          
          public class GoogleMapsGui extends JFrame {
          
              final Logger log = Logger.getLogger(GoogleMapsGui.class.getName());
              private JPanel contentPane;
          
              /**
               * Launch the application.
               */
              public static void main(String[] args) {
                  EventQueue.invokeLater(new Runnable() {
                      public void run() {
                          try {
                              GoogleMapsGui frame = new GoogleMapsGui();
                              frame.setVisible(true);
                          } catch (Exception e) {
                              e.printStackTrace();
                          }
                      }
                  });
              }
          
              /**
               * Create the frame.
               */
              public GoogleMapsGui() {
                  setTitle("Map");
                  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                  setBounds(100, 100, 592, 352);
                  contentPane = new JPanel();
                  contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
                  setContentPane(contentPane);
          
                  JFrame test = new JFrame("Google Maps");
          
                  try {
                      // String latitude = "-25.9994652";
                      // String longitude = "28.3112051";
                      String location = JOptionPane
                              .showInputDialog(" please enter the desired loccation");// get
                                                                                      // the
                                                                                      // location
                                                                                      // for
                                                                                      // geo
                                                                                      // coding
                      Scanner sc = new Scanner(location);
                      Scanner sc2 = new Scanner(location);
                      String marker = "";
                      String path = JOptionPane
                              .showInputDialog("what is your destination?");
                      String zoom = JOptionPane
                              .showInputDialog("how far in do you want to zoom?\n"
                                      + "12(zoomed out) - 20 (zoomed in)");
          
                      String imageUrl = "https://maps.googleapis.com/maps/api/staticmap?";
                      while (sc.hasNext()) {// add location to imageUrl
                          imageUrl = imageUrl + sc.next();
                      }
                      marker = "&markers=color:red|";
                      while (sc2.hasNext()) {// add marker location to marker
                          marker = marker + sc2.next() + ",";
          
                      }
                      marker = marker.substring(0, marker.length() - 1);
          
                      imageUrl = imageUrl + "&size=620x620&scale=2&maptype=hybrid"
                              + marker;
                      //
                      log.info("Generated url");
          
                      String destinationFile = "image.jpg";
          
                      // read the map image from Google
                      // then save it to a local file: image.jpg
                      //
                      URL url = new URL(imageUrl);
                      InputStream is = url.openStream();
                      OutputStream os = new FileOutputStream(destinationFile);
          
                      byte[] b = new byte[2048];
                      int length;
          
                      while ((length = is.read(b)) != -1) {
                          os.write(b, 0, length);
                      }
                      log.info("Created image.jpg");
          
                      is.close();
                      os.close();
                      sc.close();
                      sc2.close();
                      log.info("Closed util's");
                  } catch (IOException e) {
                      e.printStackTrace();
                      System.exit(1);
                      log.severe("Could not create image.jpg");
                  }// fin getting and storing image
          
                  ImageIcon imageIcon = new ImageIcon((new ImageIcon("image.jpg"))
                          .getImage().getScaledInstance(630, 600,
                                  java.awt.Image.SCALE_SMOOTH));
                  contentPane.setLayout(null);
                  JLabel imgMap = new JLabel(imageIcon);
                  imgMap.setBounds(5, 5, 571, 308);
                  contentPane.add(imgMap);
              }
          
          }
          

          还可以查看 Goolge 静态地图 API here

          【讨论】:

            猜你喜欢
            • 2016-02-21
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-12-22
            • 1970-01-01
            • 1970-01-01
            • 2023-03-21
            • 1970-01-01
            相关资源
            最近更新 更多