In short, you can think of a Map as an ordered list of Layers, where each layer is rendered on top of the previous ones according to the data it contains (the feature source) following certain painting rules (the style).

The map is represented by the MapContext interfaces, which provide methods to manage layers (that is, add, move and remove). You can add a layer by first creating an object implementing the MapLayer interface, or by directly using convenience methods that take either a FeatureSource or a FeatureCollection and a Style object.

You can set an area of interest using the setAreaOfInterest() method, which also sets the CoordinateReferenceSystem (CRS) of the MapContext. Renderers should be able to reproject data coming from another CRS into the MapContext CRS at rendering time. Unfortunately, at the moment, only the j2d.Renderer has the infrastructure to do this, and we also lack support for CoordinateReferenceSystems in the data sources. At present I'm not aware of any that can properly read the CoordinateReferenceSystem from the persistent data store (please also consider that the most common data sources, such as old style shapefiles, do not carry any CRS information).

A MapContext also has some non-geographic information such as a title, an abstract and a set of keywords. These can be used in printing functions or in graphical settings to better describe the map (they are the only metadata available at the moment at the MapContext level).

Applications interested in the MapContext and MapLayer events can listen on the set of interfaces provided, namely:

  • PropertyChangeListener for listening to metadata changes;
  • MapLayerListListener for listening to changes in the list of layers (order changes, insertion, deletions);
  • MapBoundsListener for listening to area of interest or coordinate reference system changes.

As you may have noticed, MapContext and MapLayer are just interfaces: use the DefaultMapContext and DefaultMapLayer default implementation if you don't have fancy needs, or roll your own specialized implementations otherwise.

Example of map creation with layers, feature collection and style.



//Creation of the Map
		DefaultMapContext context = new DefaultMapContext();
		
// adding the listeners if needed
		context.addMapBoundsListener(new MapBoundsListener() {
			public void mapBoundsChanged(MapBoundsEvent event) {
				// anAction
			}
		});
		context.addMapLayerListListener(new MapLayerListListener() {
			public void layerAdded(MapLayerListEvent event) {
				// anAction
			}

			public void layerRemoved(MapLayerListEvent event) {
				// anAction
			}

			public void layerChanged(MapLayerListEvent event) {
				// anAction
			}

			public void layerMoved(MapLayerListEvent event) {
				// anAction
			}
		});
		context.addPropertyChangeListener(new PropertyChangeListener() {
			public void propertyChange(PropertyChangeEvent evt) {
				// anAction
			}
		});
		
// set some extra information to the map
		context.setTitle("ATitle");
		context.setContactInformation("anInformation");
		context.setAbstract("anAbstract");

//creating feature collection
                //feature attributes creation 
		AttributeType[] types = new AttributeType[1];
		types[0] = AttributeTypeFactory.newAttributeType("centre", Point.class);

                // feature type creation
		FeatureType pointType = FeatureTypeFactory.newFeatureType(types,
				"pointfeature");

		//feature instance creation
                // we could use also the WKT writers instead 
		GeometryFactory geomFac = new GeometryFactory(); 
		Point point1 = geomFac.createPoint(new Coordinate((0.0), 0.0));
		Point point2 = geomFac.createPoint(new Coordinate((10.0), 10.0));
                Feature feature1 = pointType.create(new Object[] { point1 });
                Feature feature2 = pointType.create(new Object[] { point2 });

                //feature collection creation
                FeatureCollection fc = FeatureCollections.newCollection();
		fc.add(feature1);	
                fc.add(feature2);

// creating the map style
		StyleFactory sf = StyleFactory.createStyleFactory();
		PointSymbolizer ps = sf.getDefaultPointSymbolizer();
		Rule r = sf.createRule();
		r.setSymbolizers(new Symbolizer[] { ps });
		Style s = sf.createStyle();
		s.setName("style");
		s.addFeatureTypeStyle(sf.createFeatureTypeStyle(new Rule[] { r }));

//create the layer from the style
		MapLayer aLayer = new DefaultMapLayer(fc, s, "layer1");
		
//adding the layer to the map
		context.addLayer(aLayer);