OpenLayers V3.19.1未在JavaFX WebView中显示

时间:2016-11-08 09:37:48

标签: javafx openlayers-3

我在使用JavaFX WebView中的OpenLayers v3显示地图时遇到问题。这是我的代码:

openLayersV3.html

<!doctype html>
<html lang="en">
<head>
<link rel="stylesheet"
    href="https://openlayers.org/en/v3.19.1/css/ol.css" type="text/css">
<style>
.map {
    height: 400px;
    width: 400px;
}
</style>
<script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList"></script>
<script src="https://openlayers.org/en/v3.19.1/build/ol.js"
    type="text/javascript"></script>
<title>OpenLayers 3 example</title>
<script type="text/javascript">
        function loadMap() {
            var map = new ol.Map({
                target: 'map',
                layers: [
                  new ol.layer.Tile({
                    source: new ol.source.OSM()
                  })
                ],
                view: new ol.View({
                  center: ol.proj.fromLonLat([10.0, 53.55]),
                  zoom: 10
                })
              });
        }
    </script>
</head>
<body onLoad="loadMap()">
    <h2>My Map</h2>
    <div id="map" class="map"></div>
</body>
</html>

以下是装载机的摘录:

OsmView.java

...
protected WebView webView = new WebView();
protected WebEngine webEngine = webView.getEngine();

public OsmView() {
    final URL urlOsmMap = getClass().getResource("/some/package/name/openLayersV3.html");

    webEngine.load(urlOsmMap.toExternalForm());
    getChildren().add(webView);
}
...

当我在IE或Firefox等浏览器中加载html时,它显示没有任何复杂性。但是在Java程序中,只有一个带有h2的空白页面(“我的地图”)。但是JavaScript没有加载。那么,我在这里做错了什么?

1 个答案:

答案 0 :(得分:1)

好的,实际上我找到了一个解决方案:找不到requestAnimationFrame所以你必须在每个其他javascript之前添加以下行:

window.requestAnimFrame = (function(){
    return window.requestAnimationFrame ||
        window.webkitRequestAnimationFrame ||
        window.mozRequestAnimationFrame ||
        window.oRequestAnimationFrame ||
        window.msRequestAnimationFrame ||
        function(callback, element) {
            window.setTimeout(callback, 1000 / 60);
        };
})();
var requestAnimationFrame = window.requestAnimFrame;