Skip To Content ArcGIS for Developers Sign In Dashboard

Geodesic Operations


 * Copyright 2018 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
 * 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.

package com.esri.samples.geometry.geodesic_operations;

import java.text.DecimalFormat;
import java.util.Arrays;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Point2D;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.input.MouseButton;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import com.esri.arcgisruntime.geometry.GeodeticCurveType;
import com.esri.arcgisruntime.geometry.Geometry;
import com.esri.arcgisruntime.geometry.GeometryEngine;
import com.esri.arcgisruntime.geometry.LinearUnit;
import com.esri.arcgisruntime.geometry.LinearUnitId;
import com.esri.arcgisruntime.geometry.Point;
import com.esri.arcgisruntime.geometry.PointCollection;
import com.esri.arcgisruntime.geometry.Polyline;
import com.esri.arcgisruntime.geometry.SpatialReference;
import com.esri.arcgisruntime.geometry.SpatialReferences;
import com.esri.arcgisruntime.mapping.ArcGISMap;
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.MapView;
import com.esri.arcgisruntime.symbology.SimpleLineSymbol;
import com.esri.arcgisruntime.symbology.SimpleMarkerSymbol;

public class GeodesicOperationsSample extends Application {

  private MapView mapView;
  private final SpatialReference srWgs84 = SpatialReferences.getWgs84();

  public void start(Stage stage) {

    try {
      // create stack pane and application scene
      StackPane stackPane = new StackPane();
      Scene fxScene = new Scene(stackPane);

      // set title, size, and add scene to stage
      stage.setTitle("Geodesic Operations Sample");

      // create a map
      ArcGISMap map = new ArcGISMap(Basemap.createImagery());

      // set the map to a map view
      mapView = new MapView();

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

      // add a graphic at JFK airport to represent the flight start location
      Point start = new Point(-73.7781, 40.6413, srWgs84);
      SimpleMarkerSymbol locationMarker = new SimpleMarkerSymbol(SimpleMarkerSymbol.Style.CIRCLE, 0xFF0000FF, 10);
      Graphic startLocation = new Graphic(start, locationMarker);

      // create a graphic for the destination
      Graphic endLocation = new Graphic();

      // create a graphic representing the geodesic path between the two locations
      Graphic path = new Graphic();
      path.setSymbol(new SimpleLineSymbol(SimpleLineSymbol.Style.DASH, 0xFF0000FF, 5));

      // create a label to show the distance
      Label distanceLabel = new Label();
      DecimalFormat formatter = new DecimalFormat("#0.00 km");
      VBox labelBox = new VBox(distanceLabel);
      labelBox.setMaxSize(110, 40);

      // create a linear unit for measuring the distance
      LinearUnit unitOfMeasurement = new LinearUnit(LinearUnitId.KILOMETERS);

      // and a mouse click listener to get the user's input for the destination
      mapView.setOnMouseClicked(e -> {
        if (e.isStillSincePress() && e.getButton() == MouseButton.PRIMARY) {
          // change the end location's geometry to the clicked location
          Point2D point2D = new Point2D(e.getX(), e.getY());
          Point destination = (Point) GeometryEngine.project(mapView.screenToLocation(point2D), srWgs84);
          // create a straight line path between the start and end locations
          PointCollection points = new PointCollection(Arrays.asList(start, destination), srWgs84);
          Polyline polyline = new Polyline(points);
          // densify the path as a geodesic curve and show it with the path graphic
          Geometry pathGeometry = GeometryEngine.densifyGeodetic(polyline, 1, unitOfMeasurement, GeodeticCurveType
          // calculate the path distance
          double distance = GeometryEngine.lengthGeodetic(pathGeometry, unitOfMeasurement, GeodeticCurveType.GEODESIC);

      // add the scene view and label box to the stack pane
      stackPane.getChildren().addAll(mapView, labelBox);
      StackPane.setMargin(labelBox, new Insets(10, 0, 0, 10));
      StackPane.setAlignment(labelBox, Pos.TOP_LEFT);
    } catch (Exception e) {
      // on any error, display the stack trace.

   * Stops and releases all resources used in application.
  public void stop() {

    if (mapView != null) {

   * Opens and runs application.
   * @param args arguments passed to this application
  public static void main(String[] args) {



In this topic
  1. Code