You will learn: how to build an app that displays feature layers in a 2D map.
With the ArcGIS Runtime SDK for Java, it is easy to create a mapping app that displays a basemap and layer that is a hosted feature service. ArcGIS Online hosts a number of tile and vector tile basemaps that you can choose from, including streets, satellite, streets-vector, and streets-navigation-vector. You can also add your own datasets by adding feature layers. These layers are hosted by ArcGIS Online or by other ArcGIS servers. By default, feature layers will draw with pre-set symbols to fill the extent of the map, but you can customize this by setting your own styles with a renderer or using a SQL expression to limit the data retrieved.
In this tutorial, you will build a simple mapping app with a basemap and a feature layer that represents trailheads near Los Angeles. The layer is hosted on the Los Angeles GeoHub.
If you have not done the starter project tutorial, be sure to review it first so you are familiar with running the app.
In a new or empty folder, make a copy of the Create a starter app solution.
rootProject.name
value to add-layers-to-a-map
.Open the App.java file in src/main/java/com/arcgis/developers/labs. Then merge the following with the existing imports.
import com.esri.arcgisruntime.data.ServiceFeatureTable;
import com.esri.arcgisruntime.layers.FeatureLayer;
import com.esri.arcgisruntime.loadable.LoadStatus;
Add a private method addTrailheadsLayer
that creates a ServiceFeatureTable
to hold the trailhead data from an online feature service.
private void addTrailheadsLayer() {
if (mapView != null) {
String url = "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trailheads/FeatureServer/0";
ServiceFeatureTable serviceFeatureTable = new ServiceFeatureTable(url);
}
}
Create a FeatureLayer
using the service feature table and add it to the map as an operational layer. This will display the features above the basemap.
ServiceFeatureTable serviceFeatureTable = new ServiceFeatureTable(url);
/* ** ADD ** */
FeatureLayer featureLayer = new FeatureLayer(serviceFeatureTable);
ArcGISMap map = mapView.getMap();
map.getOperationalLayers().add(featureLayer);
To change the view to center on the feature layer's features, set the map view's viewpoint to the feature layer's extent after the layer is done loading:
map.getOperationalLayers().add(featureLayer);
/* ** ADD ** */
featureLayer.addDoneLoadingListener(() -> {
if (featureLayer.getLoadStatus() == LoadStatus.LOADED) {
mapView.setViewpointGeometryAsync(featureLayer.getFullExtent());
} else {
featureLayer.getLoadError().getCause().printStackTrace();
}
});
Your map should load and center on the Los Angeles area with a separate layer (feature layer) that shows the trailhead symbols.
Return to the data tutorial where you created the trailheads layer and add or remove trailhead locations. Rerun your app and see the layer update without any code changes.
Return to the data tutorial where you created the trailheads layer and change the symbol used for trailhead locations. Rerun your app and see the layer update without any code changes.