| Version 9 (modified by xavid, 7 years ago) |
|---|
OpenLayers JavaScript API Documentation
Some code samples: Hello World example:
<html>
<head>
<script src="http://openlayers.org/api/2/OpenLayers.js"></script>
</head>
<body>
<div style="width:100%; height:100%" id="map"></div>
<script defer="defer" type="text/javascript">
var map = new OpenLayers.Map('map');
var wms = new OpenLayers.Layer.WMS( "OpenLayers WMS",
"http://labs.metacarta.com/wms/vmap0", {layers: 'basic'} );
map.addLayer(wms);
map.zoomToMaxExtent();
</script>
</body>
</html>
Add a LayerSwitcher control:
map.addControl(new OpenLayers.Control.LayerSwitcher());
Add a TextFile Layer:
map.addLayer(new OpenLayers.Layer.Text("Data", "datafile.txt"));
Add a Markers Layer, and create a marker:
var markersLayer = new OpenLayers.Layer.Markers("Some Points of Interest");
var marker = new OpenLayers.Marker(
new OpenLayers.LonLat(5,10),
new OpenLayers.Icon(
"http://openlayers.org/api/img/marker.png",
new OpenLayers.Size(10,15)
));
markersLayer.addMarker(marker);
map.addLayer(markersLayer);
Incomplete API Documentation
This documentation needs to be fleshed out still.
- OpenLayers.Map?
- OpenLayers.Map.initialize?(div, options) -- pass a div or the id of a div, and it will be initialized as the div which will contain your map. Options modifies any of the default options in the map: maxZoomLevel, maxExtent, or maxResolution.
- OpenLayers.Map.addLayer?(layer) -- add a layer to the map. Layer must have moveTo method.
- OpenLayers.Map.addControl?(control) -- add a control to the map. Control must have a draw() method.
- OpenLayers.Map.getResolution?() -- gets the current resolution of the map in degrees/pixel.
- OpenLayers.Map.getZoom?() -- returns an integer value for the current zoom level, from 0 (zoomed all the way out) to maxZoomLevel (zoomed all the way in)
- OpenLayers.Map.getSize?() -- gets the size, in pixels, of the current div.
- OpenLayers.Map.getCenter?() -- returns the center of the map, as an OpenLayers.LonLat()
- OpenLayers.Map.getExtent?() -- returns the extent of the map, as an OpenLayers.Bounds() object
- OpenLayers.Map.getFullExtent?() -- returns the full extent of the map at max zoom level as a Bounds() object
- OpenLayers.Map.getZoomForExtent(bounds) -- given an OpenLayers.Bounds() object, returns an integer zoom level which will fit that bounds.
- OpenLayers.Map.getLatLonFromPixel?(pixel) -- given an OpenLayers.Pixel() object, returns the Lat/Lon that was clicked.
- OpenLayers.Map.setCenter(latlon, zoom) -- move the center of the map to the given OpenLayers.LonLat() and zoom level.
- OpenLayers.Map.zoomIn?() -- increase zoom level by one
- OpenLayers.Map.zoomTo?(int) -- set the zoom to the given level.
- OpenLayers.Map.zoomOut?() -- decrease zoom level by one.
- OpenLayers.Map.zoomExtent?() -- zoom to zoom level 0 *Internal functions, subject to change: OpenLayers.Map.moveToNewExtent() -- used internally by setCenter.
- OpenLayers.Layer?()
- OpenLayers.Layer.Marker?() -- must be created in order to add markers to the map
- OpenLayers.Layer.Text?() -- subclass of Layer.Marker, parses a text file and creates markers from it.
- OpenLayers.Layer.GeoRSS?() -- subclass of Layer.Marker, parses a GeoRSS file and creates markers from it.
- OpenLayers.Layer.WFS?() -- subclass of Layer.Marker, parses responses from a WFS server and creates markers from it.
- OpenLayers.Layer.WMS(name, url, params) -- Create tiled WMS layer. name is a layername, URL is WMS base URL, params is params to use, such as {map:'/mapfiles/base', 'layers':'basic'}.
- OpenLayers.Layer.KaMap(name, url, params, units)
- OpenLayers.Layer.WorldWind?(name, url, LevelZeroDegrees, zoomLevels, params)
- OpenLayers.Icon?(url, size)
- OpenLayers.Marker?(latlon, icon)
- OpenLayers.Feature?() -- not feature complete. Designed to hold data about a location, from which a Marker and Popup can be built.

