I'm trying to embed an HTML file with google maps in a JavaFX application using WebView. I tested my code with simpler HTML files (just paragraph text and divs) and it embedded correctly but for some reason google maps will not embed. Instead it displays a white rectangle with a scroll bar.
我正在尝试使用WebView将带有Google地图的HTML文件嵌入到一个JavaFX应用程序中。我用更简单的HTML文件(只有段落、文本和div)测试了我的代码,它正确地嵌入了,但出于某种原因,Google地图不能嵌入。相反,它显示一个带有滚动条的白色矩形。
Here is my HTML for google maps:
以下是我的谷歌地图的超文本标记语言:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title> GUI Embedded Map Test </title>
<style>
#map{
height:600px;
width:100%;
}
</style>
</head>
<body>
<div id="map"></div>
<script>
function initMap() {
window.map = new google.maps.Map(document.getElementById("map"), {
zoom: 15,
center:{lat:32.99069195330653, lng:-106.97436738069189},
mapTypeId: "terrain",
});
}
window.initMap = initMap;
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=RANDOMKEY&callback=initMap" /c/
></script>
</body>
</html>
(for reference, here's what it looks like when opened in a browser)
(仅供参考,以下是在浏览器中打开时的外观)
Here is my Java code for embedding it:
以下是我嵌入它的Java代码:
WebView webView = new WebView();
WebEngine webEngine = webView.getEngine();
String map_html = "";
try {
File myObj = new File(System.getProperty("user.dir")+"/src/main/resources/embeddedMap.html");
Scanner myReader = new Scanner(myObj);
while (myReader.hasNextLine()) {
map_html += myReader.nextLine();
map_html += "\n";
}
myReader.close();
} catch (FileNotFoundException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
webEngine.loadContent(map_html);
mapPane.getChildren().add(webView);
Instead of the above HTML, I've tried using iframes tags copied from the 'share' feature on google maps. I've also tried different API keys.
我尝试使用从谷歌地图上的“共享”功能复制的iframes标签,而不是上面的html。我还尝试了不同的API密钥。
Any help would be greatly appreciated!
如有任何帮助,将不胜感激!
更多回答
What you are trying to do won’t work with JavaFX 18.0.1 -> 20, see the FAQ for more detail.
您正在尝试做的事情不适用于JavaFX 18.0.1->20,有关更多详细信息,请参阅FAQ。
Workaround
解决方法
Try the latest version of JavaFX available and see if it will work. If not, try a version of JavaFX <=18 (some users have reported that such versions function OK with Google maps).
尝试最新版本的JavaFX,看看它是否能正常工作。如果没有,请尝试JavaFX <=18的版本(一些用户报告说,这样的版本在Google地图上运行正常)。
Ensure that whichever version you choose, the version of all JavaFX components is exactly the same, because mixing JavaFX component versions is not supported.
确保无论您选择哪个版本,所有JavaFX组件的版本都是完全相同的,因为不支持混合使用JavaFX组件版本。
Official support for Google Maps display in JavaFX WebView
官方支持在JavaFX WebView中显示Google地图
Google doesn’t officially support JavaFX WebView in Google Maps.
谷歌并未在谷歌地图中正式支持JavaFX WebView。
Also, there is no explicit guarantee of support the other way round from the JavaFX release notes for Google Maps in WebView.
此外,WebView中的Google Maps的JavaFX发行说明中也没有明确的支持保证。
That doesn't mean that some versions of WebView won't work with Google maps (older versions of JavaFX have been verified to work with Google maps), only that Google don't guarantee that JavaFX WebView will be supported.
这并不意味着WebView的某些版本不能在谷歌地图上运行(老版本的JavaFX已经被证实可以在谷歌地图上运行),只是谷歌不保证会支持JavaFX WebView。
FAQ (responses to comments by José Pereda)
常见问题(对何塞·佩雷达评论的回应)
And what about GMapsFX?
The last time I tried GMapsFX, it did not work either (that was probably with JavaFX 19 or 20). Internally GMapsFX relies on WebView for rendering Google Maps.
上次我尝试GMapsFX时,GMapsFX也不起作用(那可能是在JavaFX 19或20上)。在内部,GMapsFX依靠WebView来渲染谷歌地图。
The lead developer on that project comments in an issue:
该项目首席开发人员在一个问题上发表了评论:
No maps are rendered when using JavaFX 18.0.1 with its upgraded WebKit version.
Interesting, it works for me for 18 and lower, but indeed fails since 18.0.1. I wonder what change caused this, and if it is a WebKit issue, it is not that Google doesn't support WebView, but the other way around?
This is a list of clients that Google support for their JavaScript maps client API. It does not include JavaFX, if it did they would be obligated to make it work with supported versions of JavaFX, but they don’t.
这是Google为其JavaScript地图客户端API支持的客户端列表。它不包括JavaFX,如果有,他们将有义务使其与受支持的JavaFX版本一起工作,但他们不包括。
Honestly I don't think Google considers the JavaFX WebView a "true" web browser that needs to be supported. As you know, it is an embedded browser, based on the Apple Safari Port and the WebKit GTK releases, with some modifications to make it work with Java/JavaFX. It is possible that one of those modifications broke the support for Google Maps, because it stopped working only after WebKit was updated from 612.1 to 613.1 (that is between JavaFX 18 and 18.0.1).
When I originally wrote this answer I was unaware of the issue tracker report which mentions breakage of Google Maps between the JavaFX 18 and 18.0.1 release. I had instead assumed that Google had released a newer version of Google Maps that was incompatible with JavaFX. Perhaps this is not the case.
当我最初写这个答案的时候,我并不知道问题跟踪报告中提到了Google Maps在JavaFX 18和18.0.1版本之间的损坏。相反,我认为Google发布了一个与JavaFX不兼容的更新版本的Google地图。或许事实并非如此。
I do see that Google currently explicitly support (at least for the moment), embedded browsers on other platforms like the deprecated iOS UIWebView that are not "true" browsers, so it would probably be possible for them to explicitly support the JavaFX WebView if they wanted to (my opinion). It would probably be harder for the JavaFX team to explicitly support Google Maps because it can't be known if Google will make some change to their Maps API implementation that makes it incompatible with WebView.
我确实看到谷歌目前明确支持(至少目前)其他平台上的嵌入式浏览器,比如不推荐的iOS UIWebView,这些不是“真正的”浏览器,所以如果他们想显式支持JavaFX WebView(我的观点),很可能是可能的。对于JavaFX团队来说,明确支持Google Maps可能会更难,因为不知道Google是否会对他们的Maps API实现进行一些更改,使其与WebView不兼容。
更多回答
Interesting, it works for me for 18 and lower, but indeed fails since 18.0.1. I wonder what change caused this, and if it is a WebKit issue, it is not that Google doesn't support WebView, but the other way around?
有趣的是,它适用于我的18岁及以下版本,但从18.0.1开始确实失败了。我想知道是什么变化导致了这一点,如果是WebKit的问题,那不是谷歌不支持WebView,而是反过来?
Honestly I don't think Google considers the JavaFX WebView a "true" web browser that needs to be supported. As you know, it is an embedded browser, based on the Apple Safari Port and the WebKit GTK releases, with some modifications to make it work with Java/JavaFX. It is possible that one of those modifications broke the support for Google Maps, because it stopped working only after WebKit was updated from 612.1 to 613.1 (that is between JavaFX 18 and 18.0.1).
老实说,我认为谷歌并不认为JavaFX WebView是需要支持的“真正的”网络浏览器。如您所知,它是一个嵌入式浏览器,基于Apple Safari Port和WebKit GTK版本,经过一些修改使其可以与Java/JavaFX一起工作。其中一个修改可能破坏了对Google Maps的支持,因为只有在WebKit从612.1更新到613.1(即在JavaFX18和18.0.1之间)之后,Google Maps才停止工作。
Inlined comments and responses in a FAQ section.
FAQ部分中的内联评论和回复。
我是一名优秀的程序员,十分优秀!