簡體   English   中英

如何在Java Application中訪問google maps api?

[英]How to access google maps api in Java Application?

如何從Java應用程序訪問google maps API?

您可以使用Swing-WS ,JXMapViewer組件可用,並提供與JavaScript版本類似的功能。 但是,在提供的API之外訪問Google磁貼服務器仍然不合法:JavaScript和Flash。

打開了一個問題來跟蹤此請求: http//code.google.com/p/gmaps-api-issues/issues/detail?id = 1396 它已獲批准,但誰知道什么時候可以使用。

客戶端Java的最佳選擇是Static Maps API 對於服務器端Java,答案很大程度上取決於您用於開發的框架。 話雖如此,Google Maps API已有詳細記錄

你可以使用swingx的Swing Labs,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

看看上面文章中的源代碼,有三行代碼,您可以輕松查看地圖:

如果您只是尋找一個靜態地圖,您可以使用此代碼來使地圖工作:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM