简体   繁体   中英

Display Google maps using JFrame

How to display Google maps using JFrame? 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. You might try the Java-FX based WebView, but I am not sure if that is much better.

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. Also, there could be other issues with javascript compatibility and updates to the map service.

Google provides a way of downloading maps as an image:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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