2

I am tryng to develop a naviagtion map system using ArcGIS Runtime for Java, and FXML files for the view part. I am currently facing zoomButtons disabling: in some zoomlevel examples, the zoomIn button should be disabled at zoomlevel = 18 and zoomout should be disabled at zoomlevel = 0. Now I am stuck while trying to disable those buttons at several zoom levels. Can anyone help me to solve this problem? You can find the attached code below.

I have already developped the zoomIn and zoomOut methods and they are working properly.

//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();  
}
user207421
  • 305,947
  • 44
  • 307
  • 483
S M MARUF
  • 21
  • 3

1 Answers1

1

Define a property (zoomLevel) and bind the disableProperty of the buttons to the zoomLevel property when it goes above or below certain maximum and minimum values.

Zoom between min and max

zoom between min and max

Zoomed out

zoomed out

Zoomed in

zoomed in

Implementation is pretty straightforward. A sample app is provided below to demonstrate the concepts involved.

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);
    }

}
jewelsea
  • 150,031
  • 14
  • 366
  • 406