我正在尝试使用ArcGIS Java运行时和视图部分的FXML文件来开发导航地图系统。我目前正面临着禁用zoomButtons的问题:在某些zoomlevel
示例中,应在zoomIn
处禁用zoomlevel = 18
按钮,而在zoomout
处应禁用zoomlevel = 0
。现在,我在尝试在多个缩放级别禁用这些按钮时遇到了麻烦。谁能帮我解决这个问题?您可以在下面找到随附的代码。
我已经开发了zoomIn
和zoomOut
方法,它们可以正常工作。
//ZoomIn Function is created
public void zoomInFunction() {
Viewpoint current = mapView.getCurrentViewpoint(Viewpoint.Type.CENTER_AND_SCALE);
Viewpoint zoomedIn = new Viewpoint((Point) current.getTargetGeometry(), current.getTargetScale() / 2.0);
mapView.setViewpointAsync(zoomedIn);
}
//ZoomOut Function is created
public void zoomOutFunction() {
Viewpoint current = mapView.getCurrentViewpoint(Viewpoint.Type.CENTER_AND_SCALE);
Viewpoint zoomedOut = new Viewpoint((Point) current.getTargetGeometry(), current.getTargetScale() * 2.0);
mapView.setViewpointAsync(zoomedOut);
}
// Create action event for ZoomIn Function
public void zoomInAction(ActionEvent event) {
map.zoomInFunction();
}
// Create action event for ZoomOut Function
public void zoomOutAction(ActionEvent event) {
map.zoomOutFunction();
}
最佳答案
定义属性(zoomLevel
),并在按钮的disableProperty
高于或低于某些最大值和最小值时将其绑定到zoomLevel属性。
在最大和最小之间缩放
缩小
放大
实施非常简单。下面提供了一个示例应用程序来演示所涉及的概念。
import javafx.application.Application;
import javafx.beans.property.*;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
public class BoundedZoom extends Application {
private static final int MIN_ZOOM_LEVEL = 1;
private static final int MAX_ZOOM_LEVEL = 5;
private static final int DEFAULT_ZOOM_LEVEL = 3;
private IntegerProperty zoomLevel = new SimpleIntegerProperty(DEFAULT_ZOOM_LEVEL);
public int getZoomLevel() {
return zoomLevel.get();
}
public IntegerProperty zoomLevelProperty() {
return zoomLevel;
}
public void setZoomLevel(int zoomLevel) {
this.zoomLevel.set(zoomLevel);
}
@Override
public void start(Stage stage) throws Exception {
Button zoomIn = new Button("Zoom In");
zoomIn.setOnAction(event -> setZoomLevel(getZoomLevel() + 1));
zoomIn.setDisable(getZoomLevel() >= MAX_ZOOM_LEVEL);
zoomIn.disableProperty().bind(zoomLevel.greaterThanOrEqualTo(MAX_ZOOM_LEVEL));
Button zoomOut = new Button("Zoom Out");
zoomOut.setOnAction(event -> setZoomLevel(getZoomLevel() - 1));
zoomOut.setDisable(getZoomLevel() <= MIN_ZOOM_LEVEL);
zoomOut.disableProperty().bind(zoomLevel.lessThanOrEqualTo(MIN_ZOOM_LEVEL));
Label zoomLevelDescLabel = new Label("Zoom level (min " + MIN_ZOOM_LEVEL + ", max " + MAX_ZOOM_LEVEL + "): ");
Label zoomLevelValueLabel = new Label("" + getZoomLevel());
zoomLevelValueLabel.textProperty().bind(zoomLevel.asString());
Pane zoomLevelDisplay = new HBox(10, zoomLevelDescLabel, zoomLevelValueLabel);
Pane controlPane = new HBox(10, zoomIn, zoomOut);
Pane layout = new VBox(10, zoomLevelDisplay, controlPane);
layout.setPadding(new Insets(10));
stage.setScene(new Scene(layout));
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}