navigator.geolocation 不适用于我在 JavaFx 上使用的 WebEngine

问题描述

我正在尝试在我的桌面应用程序中使用 WebEngine 加载我的 localhost Html 页面

在我的 Html 页面上,有一些 JavaScript 行在单击按钮后使用 navigator.geolocation.getCurrentPosition(); 获取当前位置。

但是当我点击按钮时,navigator.geolocation.getCurrentPosition(); 没有运行。

我在浏览器上运行这个项目没有问题。

我的 javaFx 页面

public class GeolocationJavaFX extends Application{
    
    public static void main(String[] args) {
        launch(args);
    }
    
    @Override
    public void start(Stage stage) throws Exception {
            
        stage.setTitle("prova geolocation JavaFX");
            
        WebView root = new WebView();
        WebEngine engine = root.getEngine();
            
        String url = "http://localhost:8080/ProvaGoogleMapsWebJavaFx/geoAPI.html";
            
        engine.load(url);
            
        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.show();
    }
}

我的 geoAPI.html:

<!DOCTYPE html>
<html>
    <body>

        <p>Click the button to get your coordinates.</p>

        <button onclick="getLocation()">Try It</button>

        <p id="demo"></p>
        <p id="here"></p>

        <script>
            var x = document.getElementById("demo");
            var y = document.getElementById("here");

            function getLocation() {
                if (navigator.geolocation) {
                    y.innerHTML = "here";
                    navigator.geolocation.getCurrentPosition(showPosition);
                } else { 
                    x.innerHTML = "Geolocation is not supported by this browser.";
                }
            }

            function showPosition(position) {
                x.innerHTML = "Latitude: " + position.coords.latitude + 
  "<br>Longitude: " + position.coords.longitude;
            }
        </script>
    </body>
</html>

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...