Monday, January 18, 2010

Creating the Map Viewer

In order to create the viewer, you must first create a map. The OpenLayers.Map constructor requires one argument: This argument must either be an HTML Element, or the ID of an HTML element. This is the element in which the map will be placed.
var map = new OpenLayers.Map('map');
Ex. 2: Map Constructor
The next step to creating a viewer is to add a layer to the Map. OpenLayers supports many different data sources, from WMS to Yahoo! Maps to WorldWind. In this example, the WMS layer is used. The WMS layer is an example provided by MetaCarta.
var wms = new OpenLayers.Layer.WMS(
  "OpenLayers WMS",
  "http://labs.metacarta.com/wms/vmap0",
  {'layers':'basic'} );
map.addLayer(wms);
Ex. 3: Layer Constructor
The first parameter in this constructor is the URL of the WMS server. The second argument is an object containing the parameterss to be appended to the WMS request.
Finally, in order to display the map, you must set a center and zoom level. In order to zoom to fit the map into the window, you can use the zoomToMaxExtent function, which will zoom as close as possible while still fitting the full extents within the window.

No comments:

Post a Comment