简体   繁体   English

使用JFrame显示Google地图

[英]Display Google maps using JFrame

How to display Google maps using JFrame? 如何使用JFrame显示Google地图? What is the mistake in this code? 这段代码有什么错误? It's not showing proper location. 没有显示正确的位置。

import javax.swing.*;
import javax.swing.event.*;
import java.io.*;

public class hyperlink extends JFrame {

    public static void main(String arg[])throws Exception {
        new hyperlink();
    }

    public hyperlink() throws Exception {
        String s = "https://maps.google.com/maps?z=10&q=36.26577+-92.54324";
        JEditorPane pane = new JEditorPane(s);
        pane.setEditable(false);
        final JEditorPane finalpane = pane;
        pane.addHyperlinkListener(new HyperlinkListener() {
            public void hyperlinkUpdate(HyperlinkEvent r) {
                try {
                    if(r.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
                        finalpane.setPage(r.getURL());
                } catch(Exception e) {}
            }
        });
        setContentPane(new JScrollPane(pane));
        setSize(1000,1000);
        setVisible(true);
    }
}

JEditorPane was never intended to be a general purpose browsing component. JEditorPane从未打算成为通用的浏览组件。 You might try the Java-FX based WebView, but I am not sure if that is much better. 您可以尝试基于Java-FX的WebView,但是我不确定这是否更好。

If you simply want to display maps, better display them as an image. 如果您只想显示地图,最好将它们显示为图像。 You will need a browser component that will display a dynamic map from google. 您将需要一个浏览器组件,该组件将显示来自Google的动态地图。 Also, there could be other issues with javascript compatibility and updates to the map service. 此外,JavaScript兼容性和地图服务更新可能还会存在其他问题。

Google provides a way of downloading maps as an image: Google提供了一种将地图下载为图像的方式:

https://developers.google.com/maps/documentation/staticmaps/#quick_example https://developers.google.com/maps/documentation/staticmaps/#quick_example

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM