JavaFX 21.0.6 WebView在Windows 11上加载Google Maps v3.58+失败的问题求助
JavaFX 21.0.6 WebView在Windows 11上加载Google Maps v3.58+失败的问题求助
我写了一个极简的JavaFX应用,版本是21.0.6(用JDK 21.0.5编译),核心功能是通过WebView加载Google Maps。奇怪的是,在Windows 10机器上运行完全正常,但到了Windows 11上就卡壳了——地图根本加载不出来,取而代之的是“Sorry, we have no imagery here”的提示。
Java应用代码
import javafx.application.Application; import javafx.application.Platform; import javafx.scene.Scene; import javafx.scene.web.WebView; import javafx.stage.Stage; import netscape.javascript.JSObject; public class GoogleMaps extends Application { public static final void main(final String[] args) { Application.launch(args); } @Override public final void start(final Stage stage) throws Exception { var webView = new WebView(); var webEngine = webView.getEngine(); webEngine.setJavaScriptEnabled(true); webEngine.setOnAlert(event -> { if ("inject:bridge".equals(event.getData())) { final var jsWindow = (JSObject)webEngine.executeScript("window"); jsWindow.setMember("javabridge", GoogleMaps.this); webEngine.executeScript("console.log = (message) => javabridge.logConsole(message);"); } }); Platform.runLater(() -> webEngine.load(ClassLoader.getSystemClassLoader().getResource("maps.html").toString())); stage.setScene(new Scene(webView)); stage.show(); } public final void logConsole(final String message) { System.out.println("WebView: " + message); } }
配套的maps.html文件
<html> <head> <title>Google Maps</title> <style> body {margin: 0; padding: 0;} #canvas {width: 100%; height: 100%;} </style> <script> alert("inject:bridge"); </script> </head> <body> <div id='canvas'></div> <script> window.addEventListener('error', event => { const {message, filename, lineno, colno, error} = event; console.log('[Error] ' + message); }); async function initMap() { console.log("Console logging is active"); const map = new google.maps.Map(document.getElementById('canvas'), {zoom: 5, center: {lat: 52.2, lng: -1.3}, mapTypeId: 'satellite'}); } </script> <!-- notes 1, version 3.58 or greater fails to display the map and shows the message 'Sorry, we have no imagery here', v3.57 works --> <!-- 2, the API key has been removed, add '?key=xxx' with an ampersand (although, it should work in 'development mode' without a key) --> <!-- --> <script async defer src="https://maps.googleapis.com/maps/api/js?v=3.58&callback=initMap&loading=async"></script> </body> </html>
Ant构建脚本
如果有人想复现这个问题,我把自己用的Apache Ant脚本也贴出来:
<project name="Google Maps" default="jar" basedir="."> <description>Google Maps Test</description> <property environment="env"/> <property name="src" location="src"/> <property name="build" location="build"/> <property name="dist" location="."/> <property name="jarname" value="googlemaps.jar"/> <target name="init"> <tstamp/> <mkdir dir="${build}"/> </target> <target name="tidy" description="Clean up, but leave the JAR file"> <delete dir="${build}"/> </target> <target name="clean" description="Clean up"> <delete dir="${build}"/> <delete file="${dist}/${jarname}"/> </target> <target name="compile" depends="clean,init" description="Compile source files"> <echo>JavaFX Library Path: ${env.PATH_TO_JAVAFX}</echo> <javac includeantruntime="false" srcdir="${src}" destdir="${build}" modulepath="${env.PATH_TO_JAVAFX}"> <compilerarg line="--add-modules ALL-MODULE-PATH"/> <compilerarg value="-version"/> </javac> </target> <target name="jar" depends="compile" description="Build the JAR file"> <jar jarfile="${dist}/${jarname}"> <fileset dir="${build}"/> <file name="maps.html"/> <manifest> <attribute name="Main-Class" value="GoogleMaps"/> <attribute name="Multi-Release" value="true"/> </manifest> </jar> </target> <target name="run" depends="jar" description="Build execute the JAR"> <java jar="${dist}/${jarname}" modulepath="${env.PATH_TO_JAVAFX}" fork="true"> <jvmarg line="--add-modules ALL-MODULE-PATH"/> </java> </target> </project>
目前的排查情况
我检查过console.log()和DOM的window错误事件处理器,都没有输出任何错误信息。我已经折腾了好一阵子,试过换最新的JavaFX 24版本,也在多台Windows 11机器上测试,但问题依然存在。
我猜会不会是Windows 11上WebView对应的WebKit版本有兼容问题?但又觉得如果是这样的话,应该早就有人遇到并上报了吧?我搜过相关的帖子和Bug报告,但没找到匹配的案例。
真心希望有人能帮我看看是不是哪里写漏了或者犯了低级错误,或者给点排查的方向,任何建议都感激不尽!
备注:内容来源于stack exchange,提问作者Max van Daalen
相关产品推荐
相关产品推荐

