杰瑞科技汇

ArcGIS API for Java如何快速上手开发?

Of course! Here is a comprehensive guide to the ArcGIS API for Java, covering what it is, its key components, how to get started, and its primary use cases.

ArcGIS API for Java如何快速上手开发?-图1
(图片来源网络,侵删)

What is the ArcGIS API for Java?

The ArcGIS API for Java is a powerful library that allows Java developers to integrate Esri's mapping and GIS capabilities directly into their Java applications. It's not for creating web maps in a browser (like the JavaScript API); instead, it's designed for server-side, desktop, and command-line applications.

Think of it as the engine that can power complex geospatial processes and analyses behind the scenes.

Key Use Cases

You would typically use the ArcGIS API for Java when you need to:

  • Perform Geoprocessing: Execute complex geoprocessing tools (e.g., overlay analysis, network analysis, geocoding) on a server without a user interface.
  • Automate Workflows: Create scheduled tasks to update data, generate maps, or run analyses. For example, a nightly job that calculates flood risk zones.
  • Build Geospatial Services: Develop RESTful services that expose GIS functionality to other applications.
  • Integrate GIS into Desktop Applications: Embed maps and analysis tools into a custom Java Swing or JavaFX desktop application.
  • Batch Process Data: Process large datasets in an automated fashion, such as converting thousands of shapefiles to file geodatabases or reprojecting numerous datasets.

Core Concepts and Key Components

The API is built around several core concepts that you'll interact with frequently.

ArcGIS API for Java如何快速上手开发?-图2
(图片来源网络,侵删)

ArcGISRuntime

This is the foundational class for most applications. You initialize it to set up the runtime environment, manage licenses, and handle lifecycle events.

import com.esri.arcgisruntime.ArcGISRuntime;
public class MyApp {
    public static void main(String[] args) {
        // Initialize the ArcGIS Runtime with your license
        ArcGISRuntime.setLicense("your-license-key"); // Or use a license file
        // Your application logic starts here...
    }
}

Map and Basemap

A Map object represents the collection of data layers you want to display or analyze. A Basemap provides the foundational background layers (like streets, imagery, or topography).

import com.esri.arcgisruntime.mapping.ArcGISMap;
import com.esri.arcgisruntime.mapping.BasemapStyle;
// Create a map with a topographic basemap
ArcGISMap map = new ArcGISMap(BasemapStyle.ARCGIS_TOPOGRAPHIC);

Layer

Layers represent the thematic data on your map. The API supports several types:

  • FeatureLayer: Displays vector data from a feature service or a local geodatabase/shapefile.
  • ArcGISMapImageLayer: Displays map content dynamically generated from a map service.
  • RasterLayer: Displays raster or imagery data.
  • OpenStreetMapLayer: Displays data from the OpenStreetMap service.
import com.esri.arcgisruntime.layers.FeatureLayer;
import com.esri.arcgisruntime.layers.ArcGISTiledLayer;
// Add a feature layer from a service
FeatureLayer citiesLayer = new FeatureLayer(new ServiceUri("https://services.arcgis.com/.../FeatureServer/0"));
// Add a tiled layer (e.g., for streets)
ArcGISTiledLayer streetsLayer = new ArcGISTiledLayer(new Uri("https://services.arcgisonline.com/.../World_Street_Map"));

Geoprocessing

This is one of the most powerful features of the API. It allows you to execute geoprocessing tools, both local and those available from ArcGIS Enterprise or ArcGIS Online.

ArcGIS API for Java如何快速上手开发?-图3
(图片来源网络,侵删)
import com.esri.arcgisruntime.tasks.geoprocessing.GeoprocessingJob;
import com.esri.arcgisruntime.tasks.geoprocessing.GeoprocessingParameters;
import com.esri.arcgisruntime.tasks.geoprocessing.GeoprocessingTask;
// Define the task URL (e.g., an analysis service)
GeoprocessingTask gpTask = new GeoprocessingTask(new Uri("https://sampleserver6.arcgisonline.com/arcgis/rest/services/911Calls/Geoprocessing/GPServer/911CallsHotSpot"));
// Set up parameters for the tool (e.g., the input feature layer)
GeoprocessingParameters gpParams = new GeoprocessingParameters(GeoprocessingExecutionType.ExecuteAsynchronous);
gpParams.getInputs().add(new GPFeatureRecordSetLayer("911_Calls", citiesLayer));
// Submit the job
GeoprocessingJob gpJob = gpTask.createJob(gpParams);
gpJob.start();
// Check for results
gpJob.addJobStatusChangedListener(() -> {
    if (gpJob.getStatus() == JobStatus.SUCCEEDED) {
        System.out.println("Analysis complete!");
        // Process the results...
    }
});

How to Get Started (A Simple Desktop App Example)

Here’s a step-by-step guide to creating a simple Java Swing application that displays a map.

Prerequisites

  1. Java Development Kit (JDK): Version 8 or newer.
  2. An IDE: IntelliJ IDEA or Eclipse are excellent choices.
  3. ArcGIS Runtime for Java SDK: Download it from the Esri Developer website. You'll need to create a free Esri account.

Step 1: Set Up Your Project

In your IDE, create a new Java project. You will need to add the ArcGIS Runtime JAR files to your project's classpath.

  • IntelliJ: Go to File -> Project Structure -> Modules -> Dependencies. Click the icon and select JARs or directories.... Navigate to the arcgis-java-sdk-xxx\libs directory and select all the JAR files.
  • Eclipse: Right-click your project -> Build Path -> Configure Build Path... -> Libraries tab -> Add External JARs.... Select the same JAR files.

Step 2: Write the Code

Create a Java class (e.g., MapViewer.java) and paste the following code. This code creates a window with a MapView control inside it.

import com.esri.arcgisruntime.ArcGISRuntime;
import com.esri.arcgisruntime.mapping.ArcGISMap;
import com.esri.arcgisruntime.mapping.BasemapStyle;
import com.esri.arcgisruntime.mapping.view.MapView;
import javax.swing.*;
import java.awt.*;
public class MapViewer extends JFrame {
    private MapView mapView;
    public MapViewer() {
        // 1. Initialize the ArcGIS Runtime
        ArcGISRuntime.setLicense("your-license-key"); // IMPORTANT: Replace with your license
        // 2. Set up the main application window
        setTitle("ArcGIS API for Java - Simple Map Viewer");
        setSize(800, 600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());
        // 3. Create a MapView
        mapView = new MapView();
        // 4. Create a Map with a basemap
        ArcGISMap map = new ArcGISMap(BasemapStyle.ARCGIS_TOPOGRAPHIC);
        mapView.setMap(map);
        // 5. Add the MapView to the window
        add(mapView, BorderLayout.CENTER);
    }
    public static void main(String[] args) {
        // Schedule a job for the event-dispatching thread:
        // creating and showing this application's GUI.
        SwingUtilities.invokeLater(() -> {
            MapViewer viewer = new MapViewer();
            viewer.setVisible(true);
        });
    }
}

Step 3: Run the Application

If you've set up the classpath correctly and replaced "your-license-key" with a valid key, you should see a window displaying a topographic map of the world.


ArcGIS API for Java vs. Other ArcGIS APIs

It's crucial to choose the right API for your project.

Feature ArcGIS API for Java ArcGIS API for JavaScript ArcGIS API for Python
Primary Use Server-side, Desktop, Command-line automation. Client-side web applications in browsers. Data science, scripting, automation, Jupyter notebooks.
Environment Java Virtual Machine (JVM). Web Browser (requires JavaScript). Python interpreter (e.g., in a server or local machine).
UI Framework Can be used with Swing, JavaFX, or headless (no UI). HTML, CSS, and JavaScript. Primarily for scripting and data analysis; UI is limited.
Key Strength Robust geoprocessing, complex backend logic, desktop apps. Interactive web maps, widgets, and user experience. Rapid data manipulation, analysis, and machine learning integration.
License Requires ArcGIS Runtime license (free for development/production with some limitations). Free for use in public applications. Commercial licenses for private/internal apps. Free for use with Esri's Python APIs.

Learning Resources

分享:
扫描分享到社交APP
上一篇
下一篇