如何在视图中显示Gluon地图?

发布于 2025-01-24 21:58:14 字数 8238 浏览 4 评论 0原文

我正在尝试Gluon地图。我创建以下map.fxml:

<View fx:id="map" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.runtheworld.MapPresenter">
    <center>
        <Pane fx:id="pane" prefHeight="500.0" prefWidth="300.0" />
    </center>
</View>

控制器/主持人尝试显示一个地图:

public class MapPresenter {
    @FXML Pane pane;
    MapView  mapView = new MapView();
    MapPoint mapPoint = new MapPoint(56.946,24.10589);  // Riga, Latvia
    
    public void initialize() {
        StackPane sp = new StackPane();
        sp.getChildren().add(mapView);
        pane.getChildren().add(sp);
        
        // This throws error: "WARNING: sorry, would be too small". Uncommented, it crashes with IndexOutOfBoundsException
        // mapView.setZoom(9);
    }
}

视图,地图如下:

public class MapView {
    public View getView()
    {
        try {
            View view = FXMLLoader.load(MapView.class.getResource("map.fxml"));
            return view;
        } catch (IOException e) {
            System.out.println("IOException: " + e);
            return new View();
        }
    }
}

主类如下:

public class Main extends Application {
    public static final String MAP_VIEW = "map";
    private final AppManager appManager = AppManager.initialize(this::postInit);

    @Override
    public void init() {
        appManager.addViewFactory(MAP_VIEW, () -> new MapView().getView());
    }

    @Override
    public void start(Stage stage) {
        appManager.start(stage);
    }

    private void postInit(Scene scene) {
        appManager.switchView(MAP_VIEW);
    }

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

如果我不在控制器/主持人中设置Zoom,否则我将Zoom设置为一个小于6(?)的值,我会收到以下错误:“警告:对不起,太小了”,我只能放大,而不是退出。凝视代码,此错误意味着(负)缩放计算小于layoutbounds()。getheight(),该区域始终为0.0(MapView扩展了区域)。

如果我将缩放器/主持人的缩放设置为值6至10左右的值,则需要一些时间显示Gluon飞溅或空白屏幕,然后有时只有一分钟或更长时间才能最终用类似的堆栈崩溃:

Exception in thread "JavaFX Application Thread" java.lang.IndexOutOfBoundsException: Index -1 out of bounds for length 83
    at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
    at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
    at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:266)
    at java.base/java.util.Objects.checkIndex(Objects.java:359)
    at java.base/java.util.ArrayList.get(ArrayList.java:427)
    at javafx.graphics/javafx.scene.Parent.updateCachedBounds(Parent.java:1704)
    at javafx.graphics/javafx.scene.Parent.recomputeBounds(Parent.java:1648)
    at javafx.graphics/javafx.scene.Parent.doComputeGeomBounds(Parent.java:1501)
    at javafx.graphics/javafx.scene.Parent$1.doComputeGeomBounds(Parent.java:115)
    at javafx.graphics/com.sun.javafx.scene.ParentHelper.computeGeomBoundsImpl(ParentHelper.java:84)
    at javafx.graphics/com.sun.javafx.scene.NodeHelper.computeGeomBounds(NodeHelper.java:116)
    at javafx.graphics/javafx.scene.Node.updateGeomBounds(Node.java:3813)
    at javafx.graphics/javafx.scene.Node.getGeomBounds(Node.java:3775)
    at javafx.graphics/javafx.scene.Node.getLocalBounds(Node.java:3723)
    at javafx.graphics/javafx.scene.Node.updateTxBounds(Node.java:3877)
    at javafx.graphics/javafx.scene.Node.getTransformedBounds(Node.java:3669)
    at javafx.graphics/javafx.scene.Node.updateBounds(Node.java:771)
    at javafx.graphics/javafx.scene.Parent.updateBounds(Parent.java:1835)
    at javafx.graphics/javafx.scene.Parent.updateBounds(Parent.java:1833)
    at javafx.graphics/javafx.scene.Parent.updateBounds(Parent.java:1833)
    at javafx.graphics/javafx.scene.Parent.updateBounds(Parent.java:1833)
    at javafx.graphics/javafx.scene.Parent.updateBounds(Parent.java:1833)
    at javafx.graphics/javafx.scene.Parent.updateBounds(Parent.java:1833)
    at javafx.graphics/javafx.scene.Scene$ScenePulseListener.pulse(Scene.java:2530)
    at javafx.graphics/com.sun.javafx.tk.Toolkit.lambda$runPulse$2(Toolkit.java:421)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
    at javafx.graphics/com.sun.javafx.tk.Toolkit.runPulse(Toolkit.java:420)
    at javafx.graphics/com.sun.javafx.tk.Toolkit.firePulse(Toolkit.java:450)
    at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:575)
    at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:555)
    at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.pulseFromQueue(QuantumToolkit.java:548)
    at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.lambda$runToolkit$11(QuantumToolkit.java:353)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication.enterNestedEventLoopImpl(Native Method)
    at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication._enterNestedEventLoop(GtkApplication.java:360)
    at javafx.graphics/com.sun.glass.ui.Application.enterNestedEventLoop(Application.java:515)
    at javafx.graphics/com.sun.glass.ui.EventLoop.enter(EventLoop.java:107)
    at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.enterNestedEventLoop(QuantumToolkit.java:647)
    at javafx.graphics/javafx.application.Platform.enterNestedEventLoop(Platform.java:301)
    at com.gluonhq.charm.glisten.control.Dialog.lambda$installIntoMobileApplication$7(Dialog.java:605)
    at javafx.base/com.sun.javafx.binding.ExpressionHelper$SingleChange.fireValueChangedEvent(ExpressionHelper.java:181)
    at javafx.base/com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:80)
    at javafx.base/javafx.beans.property.BooleanPropertyBase.fireValueChangedEvent(BooleanPropertyBase.java:104)
    at javafx.base/javafx.beans.property.ReadOnlyBooleanWrapper.fireValueChangedEvent(ReadOnlyBooleanWrapper.java:101)
    at javafx.base/javafx.beans.property.BooleanPropertyBase.markInvalid(BooleanPropertyBase.java:111)
    at javafx.base/javafx.beans.property.BooleanPropertyBase$Listener.invalidated(BooleanPropertyBase.java:239)
    at javafx.base/com.sun.javafx.binding.ExpressionHelper$Generic.fireValueChangedEvent(ExpressionHelper.java:348)
    at javafx.base/com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:80)
    at javafx.base/javafx.beans.property.BooleanPropertyBase.fireValueChangedEvent(BooleanPropertyBase.java:104)
    at javafx.base/javafx.beans.property.BooleanPropertyBase.markInvalid(BooleanPropertyBase.java:111)
    at javafx.base/javafx.beans.property.BooleanPropertyBase.set(BooleanPropertyBase.java:145)
    at javafx.base/javafx.beans.property.BooleanProperty.setValue(BooleanProperty.java:79)
    at com.gluonhq.charm.glisten.layout.Layer.show(Layer.java:289)
    at com.gluonhq.charm.glisten.control.Dialog.showAndWait(Dialog.java:556)
    at com.gluonhq.impl.charm.glisten.util.NagScreenPresenter.lambda$showDialog$1(NagScreenPresenter.java:77)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication.lambda$runLoop$11(GtkApplication.java:290)
    at java.base/java.lang.Thread.run(Thread.java:833)

每当我在屏幕上移动光标,但长度值增加时都会重复。如果我输入12或更高的缩放值,则不会加载屏幕。

如何在视图中正确显示地图?

编辑: 下载Gluon地图代码后,我添加了几条调试线以确定可能发生的事情。在BaseMap中,在Zoom()方法中存在一个“高度”计算,该计算与父容器高度进行了比较。如果此计算大于父母的高度,则最终会出现奥术和无用的错误:“警告对不起,太小了”。

父高度似乎是一个任意值(我看到1024.0、1098.0、1261.0 ...等),而高度计算通常超过此看似任意的父高度值。

诀窍是设置您的容器大小,例如:

sp.setPrefSize(300.d,500.d);

正常服务将被恢复。

I am trying out Gluon maps. I create the following map.fxml:

<View fx:id="map" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.runtheworld.MapPresenter">
    <center>
        <Pane fx:id="pane" prefHeight="500.0" prefWidth="300.0" />
    </center>
</View>

The Controller/Presenter attempts to show a map:

public class MapPresenter {
    @FXML Pane pane;
    MapView  mapView = new MapView();
    MapPoint mapPoint = new MapPoint(56.946,24.10589);  // Riga, Latvia
    
    public void initialize() {
        StackPane sp = new StackPane();
        sp.getChildren().add(mapView);
        pane.getChildren().add(sp);
        
        // This throws error: "WARNING: sorry, would be too small". Uncommented, it crashes with IndexOutOfBoundsException
        // mapView.setZoom(9);
    }
}

The View, MapView is as follows:

public class MapView {
    public View getView()
    {
        try {
            View view = FXMLLoader.load(MapView.class.getResource("map.fxml"));
            return view;
        } catch (IOException e) {
            System.out.println("IOException: " + e);
            return new View();
        }
    }
}

The Main class is as follows:

public class Main extends Application {
    public static final String MAP_VIEW = "map";
    private final AppManager appManager = AppManager.initialize(this::postInit);

    @Override
    public void init() {
        appManager.addViewFactory(MAP_VIEW, () -> new MapView().getView());
    }

    @Override
    public void start(Stage stage) {
        appManager.start(stage);
    }

    private void postInit(Scene scene) {
        appManager.switchView(MAP_VIEW);
    }

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

If I do not set zoom in the Controller/Presenter or I set the zoom to a value less than 6 (?), I get the following error: "WARNING: sorry, would be too small" and I can only zoom in, and not back out. Peering at the code, this error implies a (negative) zoom calculation less than the LayoutBounds().getHeight() which is always 0.0 for a Region (MapView extends Region).

If I set zoom in the Controller/Presenter to a value 6 to 10 or thereabouts, it takes a time to show either a Gluon splash or a blank screen and then sometimes up to a minute or more to finally crash with a stack like so:

Exception in thread "JavaFX Application Thread" java.lang.IndexOutOfBoundsException: Index -1 out of bounds for length 83
    at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
    at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
    at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:266)
    at java.base/java.util.Objects.checkIndex(Objects.java:359)
    at java.base/java.util.ArrayList.get(ArrayList.java:427)
    at javafx.graphics/javafx.scene.Parent.updateCachedBounds(Parent.java:1704)
    at javafx.graphics/javafx.scene.Parent.recomputeBounds(Parent.java:1648)
    at javafx.graphics/javafx.scene.Parent.doComputeGeomBounds(Parent.java:1501)
    at javafx.graphics/javafx.scene.Parent$1.doComputeGeomBounds(Parent.java:115)
    at javafx.graphics/com.sun.javafx.scene.ParentHelper.computeGeomBoundsImpl(ParentHelper.java:84)
    at javafx.graphics/com.sun.javafx.scene.NodeHelper.computeGeomBounds(NodeHelper.java:116)
    at javafx.graphics/javafx.scene.Node.updateGeomBounds(Node.java:3813)
    at javafx.graphics/javafx.scene.Node.getGeomBounds(Node.java:3775)
    at javafx.graphics/javafx.scene.Node.getLocalBounds(Node.java:3723)
    at javafx.graphics/javafx.scene.Node.updateTxBounds(Node.java:3877)
    at javafx.graphics/javafx.scene.Node.getTransformedBounds(Node.java:3669)
    at javafx.graphics/javafx.scene.Node.updateBounds(Node.java:771)
    at javafx.graphics/javafx.scene.Parent.updateBounds(Parent.java:1835)
    at javafx.graphics/javafx.scene.Parent.updateBounds(Parent.java:1833)
    at javafx.graphics/javafx.scene.Parent.updateBounds(Parent.java:1833)
    at javafx.graphics/javafx.scene.Parent.updateBounds(Parent.java:1833)
    at javafx.graphics/javafx.scene.Parent.updateBounds(Parent.java:1833)
    at javafx.graphics/javafx.scene.Parent.updateBounds(Parent.java:1833)
    at javafx.graphics/javafx.scene.Scene$ScenePulseListener.pulse(Scene.java:2530)
    at javafx.graphics/com.sun.javafx.tk.Toolkit.lambda$runPulse$2(Toolkit.java:421)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
    at javafx.graphics/com.sun.javafx.tk.Toolkit.runPulse(Toolkit.java:420)
    at javafx.graphics/com.sun.javafx.tk.Toolkit.firePulse(Toolkit.java:450)
    at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:575)
    at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:555)
    at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.pulseFromQueue(QuantumToolkit.java:548)
    at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.lambda$runToolkit$11(QuantumToolkit.java:353)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication.enterNestedEventLoopImpl(Native Method)
    at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication._enterNestedEventLoop(GtkApplication.java:360)
    at javafx.graphics/com.sun.glass.ui.Application.enterNestedEventLoop(Application.java:515)
    at javafx.graphics/com.sun.glass.ui.EventLoop.enter(EventLoop.java:107)
    at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.enterNestedEventLoop(QuantumToolkit.java:647)
    at javafx.graphics/javafx.application.Platform.enterNestedEventLoop(Platform.java:301)
    at com.gluonhq.charm.glisten.control.Dialog.lambda$installIntoMobileApplication$7(Dialog.java:605)
    at javafx.base/com.sun.javafx.binding.ExpressionHelper$SingleChange.fireValueChangedEvent(ExpressionHelper.java:181)
    at javafx.base/com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:80)
    at javafx.base/javafx.beans.property.BooleanPropertyBase.fireValueChangedEvent(BooleanPropertyBase.java:104)
    at javafx.base/javafx.beans.property.ReadOnlyBooleanWrapper.fireValueChangedEvent(ReadOnlyBooleanWrapper.java:101)
    at javafx.base/javafx.beans.property.BooleanPropertyBase.markInvalid(BooleanPropertyBase.java:111)
    at javafx.base/javafx.beans.property.BooleanPropertyBase$Listener.invalidated(BooleanPropertyBase.java:239)
    at javafx.base/com.sun.javafx.binding.ExpressionHelper$Generic.fireValueChangedEvent(ExpressionHelper.java:348)
    at javafx.base/com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:80)
    at javafx.base/javafx.beans.property.BooleanPropertyBase.fireValueChangedEvent(BooleanPropertyBase.java:104)
    at javafx.base/javafx.beans.property.BooleanPropertyBase.markInvalid(BooleanPropertyBase.java:111)
    at javafx.base/javafx.beans.property.BooleanPropertyBase.set(BooleanPropertyBase.java:145)
    at javafx.base/javafx.beans.property.BooleanProperty.setValue(BooleanProperty.java:79)
    at com.gluonhq.charm.glisten.layout.Layer.show(Layer.java:289)
    at com.gluonhq.charm.glisten.control.Dialog.showAndWait(Dialog.java:556)
    at com.gluonhq.impl.charm.glisten.util.NagScreenPresenter.lambda$showDialog$1(NagScreenPresenter.java:77)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication.lambda$runLoop$11(GtkApplication.java:290)
    at java.base/java.lang.Thread.run(Thread.java:833)

which is repeated anytime I move the cursor in the screen but with an increasing length value. If I enter a zoom value of 12 or higher, no screen gets loaded.

How do I get a map to display correctly in a View?

EDIT:
After downloading the Gluon maps code, I added a couple of debug lines to determine what might be happening. In BaseMap, there is a "height" calculation occurring in the zoom() method which is compared to the parent container height. If this calculation is greater than the parent height, you end up with the arcane and useless error: "WARNING sorry, would be too small".

The parent height seems to be an arbitrary value (I see 1024.0, 1098.0, 1261.0... etc.) and the height calculation often exceeds this seemingly arbitrary parent height value.

The trick is to set your container size eg:

sp.setPrefSize(300.d,500.d);

and normal service will be resumed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文