Berling_Lin 2024-03-19 20:51 采纳率: 0%
浏览 45

在javafx中调用百度地图API绘制热力图不显示热力图图层

今天在使用百度地图API的javascriptAPI中的BMapGL绘制热力图的时候发现了个奇怪的问题。
在浏览器中打开本地编写的HTML文件后,可以正常显示HeatMap图层,但是同一个html文件在javafx窗口中加载后无法显示heatMap图层。因为目前正在做毕业设计,所以必须要在窗口组件中实现html的加载和显示热力图。所以想问一下各位程序员,这个问题如何解决?

问题预览图如下:
在窗口中无法显示heatMap图层但是可以正常显示其他控件

img


浏览器中可以正常显示heatMap图层和其他的控件

img

java的窗口显示代码如下:


package com.example.demo;

import javafx.application.Application;
import javafx.concurrent.Worker;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import netscape.javascript.JSObject;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) {
        WebView webView = new WebView();
        WebEngine webEngine = webView.getEngine();

        // 加载HTML文件
        webEngine.load(getClass().getResource("/com/example/demo/htmlPane.html").toExternalForm());

        // 监听页面加载完成事件
        webEngine.getLoadWorker().stateProperty().addListener((observable, oldValue, newValue) -> {
            if (newValue == Worker.State.SUCCEEDED) {
                // 向HTML传递参数
                System.out.println("done");
            }
        });

        StackPane root = new StackPane();
        root.getChildren().add(webView);

        primaryStage.setScene(new Scene(root, 800, 600));
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}


<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <style type="text/css">
        body, html,#allmap {
            width: 100%;
            height: 100%;
            overflow: hidden;
            margin: 0;
        }
    </style>
    <script type="text/javascript" src="https://api.map.baidu.com/api?type=webgl&v=1.0&ak=我的AK"></script>
    <script src="https://unpkg.com/mapvgl/dist/mapvgl.min.js"></script>

    <title>设置地图3D视角</title>
</head>
<body>
<div id="allmap"></div>
</body>
</html>
<script type="text/javascript">
    var map = new BMapGL.Map("allmap", {
        enableRotate: false,
        enableTilt: false
    });
    map.centerAndZoom(new BMapGL.Point(114.356585, 30.547554), 13);
    map.enableScrollWheelZoom(true);

    var circle = new BMapGL.Circle(new BMapGL.Point(114.356585, 30.547554), 1000, {strokeColor:"red",strokeWeight: 2, strokeOpacity: 1});
    var scaleCtrl = new BMapGL.ScaleControl();
    var zoomCtrl = new BMapGL.ZoomControl();
    var cityList = new BMapGL.CityListControl();

    map.addControl(scaleCtrl);
    map.addControl(zoomCtrl);
    map.addControl(cityList);
    map.addOverlay(circle);


    var data = [{geometry: {type: 'Point',coordinates: [114.2967998,30.52101608]},properties: {count:2}},{geometry: {type: 'Point',coordinates: [114.2878168,30.60497665]},properties: {count:9}},{geometry: {type: 'Point',coordinates: [114.8753086,30.95402051]},properties: {count:1}},{geometry: {type: 'Point',coordinates: [114.0255116,30.45099293]},properties: {count:9}},{geometry: {type: 'Point',coordinates: [114.2896134,30.61430107]},properties: {count:4}},{geometry: {type: 'Point',coordinates: [114.2950032,30.69818003]},properties: {count:1}},{geometry: {type: 'Point',coordinates: [114.3201558,30.69818003]},properties: {count:4}},{geometry: {type: 'Point',coordinates: [114.0201218,30.68886378]},properties: {count:2}},{geometry: {type: 'Point',coordinates: [114.3111727,30.33729167]},properties: {count:2}},{geometry: {type: 'Point',coordinates: [113.9770031,30.61274707]},properties: {count:1}},{geometry: {type: 'Point',coordinates: [114.314766,30.56922464]},properties: {count:5}},{geometry: {type: 'Point',coordinates: [114.4153762,30.51946057]},properties: {count:22}},{geometry: {type: 'Point',coordinates: [114.3506982,30.54434582]},properties: {count:5}},{geometry: {type: 'Point',coordinates: [115.0046646,30.83781474]},properties: {count:1}},{geometry: {type: 'Point',coordinates: [114.8106306,30.84556622]},properties: {count:1}},{geometry: {type: 'Point',coordinates: [114.5249694,30.48834494]},properties: {count:8}},{geometry: {type: 'Point',coordinates: [114.314766,30.628286]},properties: {count:4}},{geometry: {type: 'Point',coordinates: [114.4261558,30.45721927]},properties: {count:9}},{geometry: {type: 'Point',coordinates: [114.6309695,30.49456887]},properties: {count:1}},{geometry: {type: 'Point',coordinates: [114.2932066,30.51634946]},properties: {count:3}}];
    var heatmapLayer = new mapvgl.HeatmapLayer({size: 300, max: 20, height: 0, unit: 'm', gradient:{0.0: 'rgb(50, 50, 256)',0.1: 'rgb(50, 250, 56)',0.5: 'rgb(250, 250, 56)',1.0: 'rgb(250, 50, 56)'}});
    var view = new mapvgl.View({map:map});
    view.addLayer(heatmapLayer);
    heatmapLayer.setData(data);


</script>
  • 写回答

2条回答 默认 最新

  • 码农阿豪@新空间 Java领域优质创作者 2024-03-19 20:51
    关注
    让阿豪来帮你解答,本回答参考chatgpt3.5编写提供,如果还有疑问可以评论或留言
    问题:为什么在javafx窗口中加载HTML文件后无法显示热力图? 解答: 可能的原因:
    1. 百度地图API需要在外部引用js文件,而javafx中加载HTML文件的方式可能无法正确引用这些文件。
    2. HeatMap图层需要使用WebGL技术,在一些情况下可能无法正常工作。 解决方案:
    3. 在HTML文件中直接引用百度地图API的js文件,例如:
    <script type="text/javascript" src="https://api.map.baidu.com/api?type=webgl&v=1.0&ak=YOUR_AK"></script>
    
    1. 尝试使用其他的HTML加载方式,例如使用WebView.loadContent()或者使用JavaFX内置的浏览器引擎WebEngine,例如:
    WebEngine webEngine = webView.getEngine();
    webEngine.loadContent(htmlString);
    
    1. 如果以上方法无法解决问题,可以考虑使用其他的地图API或者其他图表库来代替热力图功能。 案例: 以下是一个使用WebEngine加载HTML文件并显示热力图的简单例子:
    import javafx.application.Application;
    import javafx.concurrent.Worker;
    import javafx.scene.Scene;
    import javafx.scene.layout.StackPane;
    import javafx.scene.web.WebEngine;
    import javafx.scene.web.WebView;
    import javafx.stage.Stage;
    public class Main extends Application {
        @Override
        public void start(Stage primaryStage) {
            WebView webView = new WebView();
            WebEngine webEngine = webView.getEngine();
            // 加载HTML文件
            webEngine.load("file:///path/to/your/htmlFile.html");
            // 监听页面加载完成事件
            webEngine.getLoadWorker().stateProperty().addListener((observable, oldValue, newValue) -> {
                if (newValue == Worker.State.SUCCEEDED) {
                    // 在页面加载完成后向HTML传递参数
                    JSObject window = (JSObject) webEngine.executeScript("window");
                    window.setMember("data", "yourData");
                }
            });
            StackPane root = new StackPane();
            root.getChildren().add(webView);
            primaryStage.setScene(new Scene(root, 800, 600));
            primaryStage.show();
        }
        public static void main(String[] args) {
            launch(args);
        }
    }
    

    在HTML文件中加载百度地图API和热力图库,并使用传递的数据来生成热力图:

    <!DOCTYPE html>
    <html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
        <style type="text/css">
            body, html, #allmap {
                width: 100%;
                height: 100%;
                overflow: hidden;
                margin: 0;
            }
        </style>
        <script type="text/javascript" src="https://api.map.baidu.com/api?type=webgl&v=1.0&ak=YOUR_AK"></script>
        <script src="https://unpkg.com/mapvgl/dist/mapvgl.min.js"></script>
        <title>显示热力图</title>
    </head>
    <body>
        <div id="allmap"></div>
        <script type="text/javascript">
            var map = new BMapGL.Map("allmap", {
                enableRotate: false,
                enableTilt: false
            });
            map.centerAndZoom(new BMapGL.Point(114.356585, 30.547554), 13);
            map.enableScrollWheelZoom(true);
            var data = window.data;
            var dataSet = new mapvgl.DataSet();
            dataSet.addData(data);
            var layer = new mapvgl.HeatMapLayer({
                dataSet: dataSet,
                colorScales: [
                    [0, "#FFFF00"],
                    [0.5, "#FF0000"]
                ],
                radius: 30,
                blur: 15
            });
            map.addLayer(layer);
        </script>
    </body>
    </html>
    
    评论

报告相同问题?

问题事件

  • 创建了问题 3月19日