Create a line with the movable end

918
7
11-21-2021 10:42 AM
VanyaIvanov
Occasional Contributor

I want to create a line with movable length. With one end is fixed and with the other end that could be dragged by mouse(increasing or decreasing the line length by dragging one end of the line by the mouse). Is it possible?

PointCollection polylinePoints = new PointCollection(SpatialReferences.getWgs84());
polylinePoints.add(new Point(0, 0));
polylinePoints.add(new Point(10, 10));//this point should be able to be dragged by mouse along the line
Polyline polyline = new Polyline(polylinePoints);
SimpleLineSymbol polylineSymbol =
new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, 0xFF0063FF, 3);
Line = new Graphic(polyline, polylineSymbol);
graphicsOverlay.getGraphics().add(Line);

 

0 Kudos
7 Replies
ABishop
MVP Regular Contributor

Hello,

First of all, where will you be using this line?  

Amanda Bishop, GISP
VanyaIvanov
Occasional Contributor

Hi, I want to use it on the Scene. (like ruler but without changing of the direction)

0 Kudos
ABishop
MVP Regular Contributor

Is there a reason why the person who is using this tool can't use a measure tool?  

Amanda Bishop, GISP
0 Kudos
VanyaIvanov
Occasional Contributor

I don't think the measure tool can help. The line should be conracted or extended by mouse along the given azimuth. The main purpose is not to only measure the distance but in the displaying of the distance along the given azimuth on the scene using the line.

0 Kudos
MarkBaird
Esri Regular Contributor

Hi @VanyaIvanov 

I'm sure this is possible, but you are going to need to write some code for this.

Just so I can understand, are you looking to extend a line so it continues to point in the same direction?

If you can share a little more information on what you are trying to achieve I'll make some suggestions.

One thing to note if if you were drawing in MapViews (2D), the Sketch Editor might help you.

Mark

VanyaIvanov
Occasional Contributor

Hi, Mark. Yes the line should continues to point in the same direction. The direction should be provided by the azimuth. So first of all the line is drawn on the Scene with starting point, given azimuth and the primary length. Then the length of the line could be changed(extended or reduced) by mouse but still in the same direction.

0 Kudos
ColinAnderson1
Esri Contributor

Here's a basic example - the line extends from lat/lon 0.0, 0.0 across the surface of the globe to where ever the mouse is. To do this properly you should create your own interaction listener on the scene view as that way you can add mouse interactions with less chance of breaking the existing built in interactions. Also there's no error checking and there are probably optimizations possible such as not having to create a new graphic/clear old graphics each move.

If you only want to change the length of the line on a fixed azimuth then you'll need to do a little more code e.g. decide how the mouse point is interpreted to give your new length.

 

 

 

import java.util.List;

import com.esri.arcgisruntime.geometry.Point;
import com.esri.arcgisruntime.geometry.PointCollection;
import com.esri.arcgisruntime.geometry.Polyline;
import com.esri.arcgisruntime.geometry.SpatialReferences;
import com.esri.arcgisruntime.mapping.ArcGISScene;
import com.esri.arcgisruntime.mapping.Basemap;
import com.esri.arcgisruntime.mapping.view.Graphic;
import com.esri.arcgisruntime.mapping.view.GraphicsOverlay;
import com.esri.arcgisruntime.mapping.view.SceneView;
import com.esri.arcgisruntime.symbology.ColorUtil;
import com.esri.arcgisruntime.symbology.SimpleLineSymbol;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.geometry.Point2D;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class App extends Application {

  private SceneView sceneView = new SceneView();
    
  @Override
  public void start(Stage stage) {
    Platform.setImplicitExit(true);

    var borderPane = new BorderPane();

    var scene = new Scene(borderPane);
    stage.setScene(scene);
    stage.setWidth(500);
    stage.setHeight(500);
    stage.show();

    var map = new ArcGISScene(Basemap.createDarkGrayCanvasVector());

    sceneView.setArcGISScene(map);
    borderPane.setCenter(sceneView);

    var graphicsOverlay = new GraphicsOverlay();
    sceneView.getGraphicsOverlays().add(graphicsOverlay);

    // add a mouse move listener
    sceneView.setOnMouseMoved(m -> {
      // convert the mouse point to screen coords
      var point = sceneView.screenToBaseSurface(new Point2D(m.getX(), m.getY()));
      
      // create a polyline from (0.0, 0.0) to the mouse point
      var pointCollection = new PointCollection(SpatialReferences.getWgs84());
      pointCollection.addAll(List.of(new Point(0.0, 0.0), point));
      var ployline = new Polyline(pointCollection);
      
      // create and show the graphic
      graphicsOverlay.getGraphics().clear();
      var graphic = new Graphic(ployline, new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID,
        ColorUtil.colorToArgb(Color.RED), 5.0f));
      graphicsOverlay.getGraphics().add(graphic);
    });
  }

  @Override
  public void stop() {
    if (sceneView != null) {
      sceneView.dispose();
    }
  }

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