"未捕获的ReferenceError:未定义地图"在使用jxbrowser的swing应用程序上

时间:2016-03-11 06:34:58

标签: java swing google-maps jxbrowser

这是使用jxbrowser在swing应用程序上显示地图的代码。但是,当我点击按钮时,"未捕获的ReferenceError:地图未定义"显示此错误。该应用程序显示地图。但是输出和标记按钮中的joom不起作用。我该怎么办?

    final Browser browser = new Browser();
    BrowserView view = new BrowserView(browser);

    JButton zoomInButton = new JButton("Zoom In");
    zoomInButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (zoomValue < MAX_ZOOM) {
                browser.executeJavaScript("map.setZoom(" + ++zoomValue + ")");
            }
        }
    });

    JButton zoomOutButton = new JButton("Zoom Out");
    zoomOutButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (zoomValue > MIN_ZOOM) {
                browser.executeJavaScript("map.setZoom(" + --zoomValue + ")");
            }
        }
    });

    JButton setMarkerButton = new JButton("Set Marker");
    setMarkerButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            browser.executeJavaScript("var myLatlng = new google.maps.LatLng(48.4431727,23.0488126);\n" +
                    "var marker = new google.maps.Marker({\n" +
                    "    position: myLatlng,\n" +
                    "    map: map,\n" +
                    "    title: 'Hello World!'\n" +
                    "});");
        }
    });

    JPanel toolBar = new JPanel();
    toolBar.add(zoomInButton);
    toolBar.add(zoomOutButton);
    toolBar.add(setMarkerButton);

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    frame.add(view, BorderLayout.CENTER);

    frame.add(toolBar, BorderLayout.SOUTH);
    frame.setSize(900, 500);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);

    // Provide the correct full path to the map.html file, e.g. D:\\map.html
    browser.loadURL("file:///E:/Programming/map.html");

和我的map.html文件:

<!DOCTYPE html>
<html>
<head>
<script src="http://maps.googleapis.com/maps/api/js"></script>
<script>
function initialize() {
  var mapProp = {
    center:new google.maps.LatLng(51.508742,-0.120850),
    zoom:5,
    mapTypeId:google.maps.MapTypeId.ROADMAP
  };
  var map=new google.maps.Map(document.getElementById("googleMap"),mapProp);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>

<body>
<div id="googleMap" style="width:850px;height:380px;"></div>
</body>

</html>

2 个答案:

答案 0 :(得分:1)

我认为这是因为您未按照https://jxbrowser.support.teamdev.com/support/solutions/articles/9000012874-google-maps

中的说明为Google Maps API提供API_KEY

请将API_KEY字符串替换为您的Google API KEY值:

https://maps.googleapis.com/maps/api/js?key= API_KEY &安培;传感器=假

答案 1 :(得分:0)

一旦页面已加载,您就无法加载API。

google.maps.event.addDomListener(window, 'load', initialize);

您可以查看一些类似的SOthis以获得更多指导。