There are two common roles for layers:
- Basemap layers - provide context (a background) for your data
- Operational layers - the map data actively in use
For example, a map of a utility network might show pipes in an operational layer and a street map background (basemap) to make it clear which houses those pipes serve.
If you want to quickly visualize data that's available to your app, but don't want to store it in an ArcGIS portal or a layer, consider using graphics.
Maps and scenes expose an operational layers property, which is a collection of layers. You can add and remove layers from the map's operational layers.
ArcGISMap map = new ArcGISMap();
Basemaps exposes a 'base layers' property, which contains all of the layers in the basemap. The getBaseLayers method can be accessed through the basemap on maps and scenes. Note that using a basemap for all your maps is recommended, when a suitable one is available. Some examples of basemaps that can be created using static factory methods are:
- World imagery
- World streets
- National geographic
- World navigation (vector)
- World streets (vector)
// Use the navigation basemap constructor
ArcGISMap map = new ArcGISMap(Basemap.createNavigationVector());
Adding a layer to muliple maps or scenes
A layer can be added only once to a single map or scene. If you add a layer already in one map or scene to another map or scene then an exception will be thrown with the message: object is already owned (see ArcGIS Runtime errorsTo use the same layer in more than one map or scene you have a few options:
- If you need the layer to be in two map or scene objects (or one of each!) you can add a copy of the layer to your second map or scene.
// add the layer to the operational layers of the first map firstMap.getOperationalLayers().add(featureLayer); // add a copy of the layer to the operational layers of the second map secondMap.getOperationalLayers().add(featureLayer.copy());
Note:The copy/clone method takes deep copy of the specific layer type, rather than the Layer base class. Any changes made to the original layer will not be reflected in the copied version of the layer.
- Create a new layer from the original source data. This is not the recommended approach because it requires accessing the underlying data source twice. For example, in the case of layers made from an online source, it would mean accessing the associated service twice.
- If the layer is no longer required in the first map or scene, remove the layer from the first map or scene and add it to the second map or scene.