|
| 1 | +/* |
| 2 | + * Copyright 2018 Esri. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.esri.samples.geometry.project; |
| 18 | + |
| 19 | +import java.text.DecimalFormat; |
| 20 | + |
| 21 | +import javafx.application.Application; |
| 22 | +import javafx.geometry.Point2D; |
| 23 | +import javafx.scene.Scene; |
| 24 | +import javafx.scene.input.MouseButton; |
| 25 | +import javafx.scene.layout.StackPane; |
| 26 | +import javafx.stage.Stage; |
| 27 | + |
| 28 | +import com.esri.arcgisruntime.geometry.Envelope; |
| 29 | +import com.esri.arcgisruntime.geometry.Geometry; |
| 30 | +import com.esri.arcgisruntime.geometry.GeometryEngine; |
| 31 | +import com.esri.arcgisruntime.geometry.Point; |
| 32 | +import com.esri.arcgisruntime.geometry.SpatialReferences; |
| 33 | +import com.esri.arcgisruntime.mapping.ArcGISMap; |
| 34 | +import com.esri.arcgisruntime.mapping.Basemap; |
| 35 | +import com.esri.arcgisruntime.mapping.view.Callout; |
| 36 | +import com.esri.arcgisruntime.mapping.view.Graphic; |
| 37 | +import com.esri.arcgisruntime.mapping.view.GraphicsOverlay; |
| 38 | +import com.esri.arcgisruntime.mapping.view.MapView; |
| 39 | +import com.esri.arcgisruntime.symbology.SimpleMarkerSymbol; |
| 40 | + |
| 41 | +public class ProjectSample extends Application { |
| 42 | + |
| 43 | + private MapView mapView; |
| 44 | + |
| 45 | + @Override |
| 46 | + public void start(Stage stage) { |
| 47 | + |
| 48 | + try { |
| 49 | + // create stack pane and application scene |
| 50 | + StackPane stackPane = new StackPane(); |
| 51 | + Scene scene = new Scene(stackPane); |
| 52 | + |
| 53 | + // set title, size, and add scene to stage |
| 54 | + stage.setTitle("Project Sample"); |
| 55 | + stage.setWidth(800); |
| 56 | + stage.setHeight(700); |
| 57 | + stage.setScene(scene); |
| 58 | + stage.show(); |
| 59 | + |
| 60 | + // create a map with a web mercator basemap |
| 61 | + ArcGISMap map = new ArcGISMap(SpatialReferences.getWebMercator()); |
| 62 | + map.setBasemap(Basemap.createNationalGeographic()); |
| 63 | + mapView = new MapView(); |
| 64 | + mapView.setMap(map); |
| 65 | + |
| 66 | + // zoom to Minneapolis |
| 67 | + Geometry startingEnvelope = new Envelope(-10995912.335747, 5267868.874421, -9880363.974046, 5960699.183877, |
| 68 | + SpatialReferences.getWebMercator()); |
| 69 | + mapView.setViewpointGeometryAsync(startingEnvelope); |
| 70 | + |
| 71 | + // create a graphics to show the input location |
| 72 | + GraphicsOverlay graphicsOverlay = new GraphicsOverlay(); |
| 73 | + mapView.getGraphicsOverlays().add(graphicsOverlay); |
| 74 | + |
| 75 | + // create a red marker symbol for the input point |
| 76 | + final SimpleMarkerSymbol markerSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbol.Style.CIRCLE, 0xFFFF0000, 5); |
| 77 | + Graphic inputPointGraphic = new Graphic(); |
| 78 | + inputPointGraphic.setSymbol(markerSymbol); |
| 79 | + graphicsOverlay.getGraphics().add(inputPointGraphic); |
| 80 | + |
| 81 | + DecimalFormat decimalFormat = new DecimalFormat("#.00000"); |
| 82 | + |
| 83 | + // show the input location where the user clicks on the map |
| 84 | + mapView.setOnMouseClicked(e -> { |
| 85 | + if (e.isStillSincePress() && e.getButton() == MouseButton.PRIMARY) { |
| 86 | + Point2D point2D = new Point2D(e.getX(), e.getY()); |
| 87 | + // show the clicked location on the map with a graphic |
| 88 | + Point originalPoint = mapView.screenToLocation(point2D); |
| 89 | + inputPointGraphic.setGeometry(originalPoint); |
| 90 | + // project the web mercator point to WGS84 |
| 91 | + Point projectedPoint = (Point) GeometryEngine.project(originalPoint, SpatialReferences.getWgs84()); |
| 92 | + // show the original and projected point coordinates in a callout from the graphic |
| 93 | + Callout callout = mapView.getCallout(); |
| 94 | + callout.setTitle("Projection"); |
| 95 | + String ox = decimalFormat.format(originalPoint.getX()); |
| 96 | + String oy = decimalFormat.format(originalPoint.getY()); |
| 97 | + String px = decimalFormat.format(projectedPoint.getX()); |
| 98 | + String py = decimalFormat.format(projectedPoint.getY()); |
| 99 | + callout.setDetail("Original: " + ox + ", " + oy + "\n" + "Projected: " + px + ", " + py); |
| 100 | + callout.showCalloutAt(inputPointGraphic, originalPoint); |
| 101 | + } |
| 102 | + }); |
| 103 | + |
| 104 | + // add the map view to the stack pane |
| 105 | + stackPane.getChildren().add(mapView); |
| 106 | + } catch (Exception e) { |
| 107 | + // on any error, display the stack trace. |
| 108 | + e.printStackTrace(); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Stops and releases all resources used in application. |
| 114 | + */ |
| 115 | + @Override |
| 116 | + public void stop() { |
| 117 | + |
| 118 | + if (mapView != null) { |
| 119 | + mapView.dispose(); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Opens and runs application. |
| 125 | + * |
| 126 | + * @param args arguments passed to this application |
| 127 | + */ |
| 128 | + public static void main(String[] args) { |
| 129 | + |
| 130 | + Application.launch(args); |
| 131 | + } |
| 132 | + |
| 133 | +} |
0 commit comments