When I developed the map sketch process, sketchEditor did not appear in the viewport of the map

495
2
07-19-2022 09:57 PM
yw19841107
New Contributor

When I developed the map sketch process, sketchEditor did not appear in the viewport of the map,Here is my code, please help me, thanks

package com.mycompany.app;

//Copyright 2020 Esri
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at
//
//http://www.apache.org/licenses/LICENSE-2.0
//
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.

import javafx.fxml.FXML;
import javafx.geometry.Point2D;
import javafx.scene.control.Button;

import com.esri.arcgisruntime.ArcGISRuntimeEnvironment;
import com.esri.arcgisruntime.mapping.ArcGISMap;
import com.esri.arcgisruntime.mapping.BasemapStyle;
import com.esri.arcgisruntime.mapping.Viewpoint;
import com.esri.arcgisruntime.mapping.view.Graphic;
import com.esri.arcgisruntime.mapping.view.GraphicsOverlay;
import com.esri.arcgisruntime.mapping.view.MapView;
import com.esri.arcgisruntime.mapping.view.SketchEditor;
import com.esri.arcgisruntime.symbology.SimpleFillSymbol;
import com.esri.arcgisruntime.symbology.SimpleLineSymbol;
import com.esri.arcgisruntime.symbology.SimpleMarkerSymbol;

import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class SketchOnMapController2 extends Application {

@FXML
private Button redoButton;
@FXML
private Button undoButton;
@FXML
private Button clearButton;
@FXML
private Button saveButton;
@FXML
private Button editButton;
@FXML
private Button stopButton;

private SketchEditor sketchEditor;
private GraphicsOverlay graphicsOverlay;
private Graphic graphic;
private SimpleFillSymbol fillSymbol;
private SimpleLineSymbol lineSymbol;
private SimpleMarkerSymbol pointSymbol;
private MapView mapView;

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

@Override
public void start(Stage stage) {
// set the title and size of the stage and show it
stage.setTitle("Display a map tutorial");
stage.setWidth(800);
stage.setHeight(700);
stage.show();

// create a JavaFX scene with a stack pane as the root node, and add it to the scene
StackPane stackPane = new StackPane();
Scene scene = new Scene(stackPane);
stage.setScene(scene);

// Note: it is not best practice to store API keys in source code.
// The API key is referenced here for the convenience of this tutorial.
String yourApiKey = "AAPKe82436f57ac0479e84c38cd64354c2b7OJpYBvfcCaLME8*******************";
ArcGISRuntimeEnvironment.setApiKey(yourApiKey);

// create a map view to display the map and add it to the stack pane
mapView = new MapView();
stackPane.getChildren().add(mapView);

ArcGISMap map = new ArcGISMap(BasemapStyle.ARCGIS_IMAGERY_STANDARD);

// set the map on the map view
mapView.setMap(map);

mapView.setViewpoint(new Viewpoint(34.02700, -118.80543, 144447.638572));

//set a viewpoint on the map view
mapView.setViewpoint(new Viewpoint(64.3286, -15.5314, 72223));

// create a graphics overlay for the graphics
graphicsOverlay = new GraphicsOverlay();

// add the graphics overlay to the map view
mapView.getGraphicsOverlays().add(graphicsOverlay);

// create a new sketch editor and add it to the map view
sketchEditor = new SketchEditor();
mapView.setSketchEditor(sketchEditor);

 

// red square for points
pointSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbol.Style.SQUARE, 0xFFFF0000, 20);
// thin green line for polylines
lineSymbol = new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, 0xFF64c113, 4);
// blue outline for polygons
SimpleLineSymbol polygonLineSymbol = new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, 0xFF1396c1, 4);
// cross-hatched interior for polygons
fillSymbol = new SimpleFillSymbol(SimpleFillSymbol.Style.CROSS, 0x40FFA9A9, polygonLineSymbol);

// add a listener for when sketch geometry is changed
sketchEditor.addGeometryChangedListener(SketchGeometryChangedListener -> {
stopButton.setDisable(false);
// save button enable depends on if the sketch is valid. If the sketch is valid then set disable opposite of true
saveButton.setDisable(!sketchEditor.isSketchValid());
undoButton.setDisable(!sketchEditor.canUndo());
redoButton.setDisable(!sketchEditor.canUndo());
});

}

/**
* Stops and releases all resources used in application.
*/
@Override
public void stop() {
if (mapView != null) {
mapView.dispose();
}
}

}

0 Kudos
2 Replies
ColinAnderson1
Esri Contributor

Hi,

It looks like your code is missing some elements. For example, I don't see where you load the FXML that defines your UI so the buttons won't appear. I would suggest trying to structure it more like the sample code. 

 

With your current code if you call 

sketchEditor.start()

after you have set the sketch editor on the view you can probably begin sketching but the buttons etc. won't work.

 

Colin

0 Kudos
yw19841107
New Contributor

Thank you for your answer. I have successfully solved this problem. Refer to JavaFX tutorial

0 Kudos