OpenStreetMap is an open source map of the world that is freely editable by the public. For more information on the OpenStreetMap project, please visit the OpenStreetMap wiki. The ArcGIS Runtime SDK for Java allows you to easily create an OpenStreetMap layer and add it to your map as a basemap layer. You can create an OpenStreetMapLayer using the default map style, or you can create a custom OpenStreetMapLayer.

Using the default OpenStreetMap layer

The simplest way to use the OpenStreetMapLayer class is to construct a layer from its default parameter-less constructor and adding it to the layer list of the map. If you have not already done so, create a Java application with a map as described in the topic Creating a Java map application.

In the Java map application template, an ArcGISTiledMapServiceLayer is created and added to the map's layer list. Replace those two lines of code with the following:

OpenStreetMapLayer osmLayer = new OpenStreetMapLayer();

The OpenStreetMapLayer should now display as your basemap layer when you run the application. The layer should look similar to the 'Standard' map style on the OpenStreetMap online interactive map. An attribution element should be showing by default in your map.

licenseTile usage policy

Using a custom OpenStreetMap layer

It is also possible to create an OpenStreetMapLayer using custom tileserver URLs which follow the OpenStreetMap tile naming conventions. You will need to use this strategy if you want to display map tiles other than those of the 'Standard' (also known as 'Mapnik') style on OpenStreetMap, such as OpenCycleMap. Some map tile providers will require a key to be obtained and set on the layer in order to display their tiles. This can be done with the setKey() method. The code sample below shows how to create an OpenStreetMapLayer using these options.

ArrayList<String> tileServerURLs = new ArrayList<String>();
tileServerURLs.add("<tile server URL>");
// add more URLs as required ...

String attribution = "My Attribution String"; // see the note above for guidelines on the attribution string
int minZoomLevel = 0;
int maxZoomLevel = 18;

OpenStreetMapLayer osmLayer = new OpenStreetMapLayer(tileServerURLs, minZoomLevel, maxZoomLevel, attribution);
// optionally, set a key if it is required by the tile provider
osmLayer.setKey("<key string>");

Sample code

Interactive sample applications which use the methods discussed above are available in the Java Sample Application under the table of content heading 'Datasources'.