Openlayers学习札记——Controls类

Openlayers学习笔记——Controls类Controls affect the display or behavior of the map.They allow everyth

Openlayers学习笔记——Controls类
Controls affect the display or behavior of the map.  They allow everything from panning and zooming to displaying a scale indicator.  Controls by default are added to the map they are contained within however it is possible to add a control to an external div by passing the div in the options parameter. API中的描述
Controls是用来控制地图的,主要功能如下:
1.用户与地图交互,如pan、Panto、zoomIn、ZoomOut;
2.在地图map上添加修饰,如Navigation、ToolBar、scaleLine;

一 如何应用Controls?
   首先我们看看Controls的类型分类,在Openlayers中多数Controls直接可以在地图上添加,比如Navigation(导航栏),第二类是需要放在Div元素中才能用,这点与地图map很相似,第三类需要放置在panel(面板)中的操作类似于网页HTML中button按钮,需要点击或绑定才能起作用,最后一类就是自定义类型的。
  如何在地图中添加Controls? 步骤如下:
   1.添加Controls相关的javascript包,实例化地图对象map和Controls对象;
   2.利用map对象的addControl()或addControls()方法在地图上添加Controls对象;

   在地图map实例化时,四中Controls会自动添加到地图map中去,分别为Openlayers.Control.Navigation(导航栏)、Openlayers.Control.PanZoom(向左向右,放大缩小等),Openlayers.Control.ArgParser(允许用户缩放到指定位置,根据Url打开/关闭地图),Openlayers.Control.Attribution(在地图上相应图层添加属性)

二 实例
   创建不含Controls的地图对象
   1.需要创建一个包含空数组control对象的地图对象
      map=new Openlayers.Map('map_element',{control:[]});

   2.地图对象中添加layer

var wms_layer = new OpenLayers.Layer.WMS(
  'WMS Layer Title',
  'http://vmap0.tiles.osgeo.org/wms/vmap0',
  {layers: 'basic'},
  {}
);
map.addLayer(wms_layer);
  
   3.创建Control对象填充Controls数组

   var navigation_control = new OpenLayers.Control.Navigation({});
var controls_array = [
  navigation_control,
  new OpenLayers.Control.PanZoomBar({}),
  new OpenLayers.Control.LayerSwitcher({}),
  new OpenLayers.Control.Permalink(),
  new OpenLayers.Control.MousePosition({})
];

  4.在地图map对象上添加Controls

  map = new OpenLayers.Map('map_element', {
  controls: controls_array
});