首页 文章

将信息从JavaFX传递到Javascript

提问于
浏览
0

我正在尝试将信息(lattiude,经度)传递到html webview中的谷歌 Map . 问题是应用程序无法在netbeans中启动 . 它返回:

使用平台C:\ Program Files \ Java \ jdk1.8.0_05 \ jre / bin / java执行C:\ Users \ Carlos \ Documents \ NetBeansProjects \ OpenPilot \ dist \ run1883323097 \ OpenPilot.jar应用程序启动方法java.lang中的异常.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang .reflect.Method.invoke(Method.java:483)at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:367)at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:305) )sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang.reflect . sun.launc上的Method.invoke(Method.java:483) her.LauncherHelper $ FXHelper.main(LauncherHelper.java:767)引起:java.lang.RuntimeException:com中的com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:894)的Application start方法中的异常 . sun.javafx.application.LauncherImpl.access $ 000(LauncherImpl.java:56)at com.sun.javafx.application.LauncherImpl $ 1.run(LauncherImpl.java:158)at java.lang.Thread.run(Thread.java: 745)引起:javafx.fxml.LoadException:javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2617)的未知路径,位于javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2595)的javafx.fxml.FXMLLoader . 在com.sun.javafx.application的com.sun.javafx.application.LauncherImpl $ 8.run(LauncherImpl.java:837)的openpilot.OpenPilot.start(OpenPilot.java:29)上加载(FXMLLoader.java:2425) . 在com.sun.javafx.application.PlatformImpl的PlatformImpl $ 7.run(PlatformImpl.java:335)com.sun.javafx.application.PlatformImpl的$ 6 $ 1.run(PlatformImpl.java:301)$ 6 $ 1.run(PlatformImpl.java) :298)在java.security.Ac com.sun.javafx.application.PlatformImpl $ 6.run(PlatformImpl.java:298)上的com.sun.glass.ui.InvokeLaterDispatcher $ Future.run(InvokeLaterDispatcher.java:95)中的cessController.doPrivileged(Native Method) .sun.glass.ui.win.WinApplication._runLoop(Native Method)位于com.sun.glass.ui.win.WinApplication.access $ 300(WinApplication.java:39),位于com.sun.glass.ui.win.WinApplication $ 4 $ 1.run(WinApplication.java:112)... 1更多引起:netscape.javascript.JSException:TypeError:'undefined'不是com.sun.webkit.dom.JSObject.fwkMakeException(JSObject.java)中的函数:128)at com.sun.webkit.WebPage.twkExecuteScript(Native Method)at com.sun.webkit.WebPage.executeScript(WebPage.java:1410)at javafx.scene.web.WebEngine.executeScript(WebEngine.java:934 )at openpilot.FXMLDocumentController.initialize(FXMLDocumentController.java:74)at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)... 13 more异常运行应用程序openpilot.OpenPilot Java结果:1

这里的代码:

public class FXMLDocumentController implements Initializable {

    @FXML public WebView Map;





    @Override
    public void initialize(URL url, ResourceBundle rb) {

        URL MapURL = getClass().getResource("map.html");
        WebEngine WebEngine = Map.getEngine();
        WebEngine.load(MapURL.toExternalForm());
        String test = "40.3130432088809";
       WebEngine.executeScript("getCoordinates('40.3130432088809')");

    }    

}

这里的html与 Map :

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <style>
      html, body, #map-canvas {
        height: 100%;
        margin: 0px;
        padding: 0px
      }
    </style>
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
    <script>
        var latitude;
        var longitude;
        function getCoordinates(latitude){
  this.latitude = latitude;

}
        var map;
        function initialize() {
            var mapOptions = {
                zoom: 8,
                center: new google.maps.LatLng(latitude, -2.900390625)
            };
        map = new google.maps.Map(document.getElementById('map-canvas'),
        mapOptions);

        google.maps.event.addListener(map, 'click', function(event) {
   placeMarker(event.latLng);
});


}
function placeMarker(location) {
    var marker = new google.maps.Marker({
        position: location, 
        map: map
    });

    google.maps.event.addListener(marker, 'rightclick', function() {
                    deleteMarker(marker);
                });

}

function deleteMarker(marker) {
                marker.setMap(null); 
            }
google.maps.event.addDomListener(window, 'load', initialize);

    </script>
  </head>
  <body>
    <div id="map-canvas"></div>
  </body>
</html>

1 回答

  • 1

    您几乎肯定会在页面加载之前尝试执行javascript . 您需要指定一个处理程序,以便在Web引擎完成加载时执行脚本 .

    @Override
    public void initialize(URL url, ResourceBundle rb) {
    
        URL MapURL = getClass().getResource("map.html");
        WebEngine WebEngine = Map.getEngine();
        WebEngine.getLoadWorker().stateProperty().addListener((ov, oldState, newState) -> {
            if (newState == State.SUCCEEDED) {
                WebEngine.executeScript("getCoordinates('40.3130432088809')");
            }
        });
        WebEngine.load(MapURL.toExternalForm());
    
    }
    

相关问题