Advertisement
Guest User

mapserver.js

a guest
May 25th, 2011
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. // Parameters to be passed either from config file or some other source
  3. var mapPath="/cgi-bin/mapserv?map=/home/mark/htdocs/sandbox/contra_costa/mapdata/ccc.map";
  4. var map, base;
  5.  
  6. function initMap(obj){
  7.   var maxBounds=obj;
  8.   var extent = new OpenLayers.Bounds(maxBounds[0], maxBounds[1], maxBounds[2], maxBounds[3]);
  9.   var options = {
  10.     restrictedExtent: extent
  11.   };
  12.   map = new OpenLayers.Map( 'map', options );
  13.   base = new OpenLayers.Layer.WMS("Contra Costa County",
  14.                   mapPath,
  15.                   {
  16.                     layers: 'county,city,water,landmarks,roads'
  17.                   },
  18.                   {
  19.                     isBaseLayer: true,
  20.                     maxResolution: "auto",
  21.                     maxExtent: extent
  22.                   });
  23.   var sirens = new OpenLayers.Layer.Vector("CWS Sirens",
  24.                        {
  25.                      strategies: [new OpenLayers.Strategy.Fixed()],
  26.                          protocol: new OpenLayers.Protocol.HTTP({
  27.                          url: "./sirens.kml",
  28.                          format: new OpenLayers.Format.KML(
  29.                          {
  30.                            extractStyles: true,
  31.                            extractAttributes: true,
  32.                            maxDepth: 1
  33.                          })
  34.                        })
  35.                        });
  36.   var tens = new OpenLayers.Layer.WMS( "TENS Zones",
  37.                     mapPath,
  38.                     {
  39.                       layers: 'tens',
  40.                       transparent: true
  41.                     }, {
  42.                       isBaseLayer: false
  43.                     });
  44.   tens.setVisibility(false);
  45.  
  46.   map.addLayers([base,sirens,tens]);
  47.  
  48.   select = new OpenLayers.Control.SelectFeature(sirens);
  49.   sirens.events.on({
  50.     "featureselected": onFeatureSelect,
  51.     "featureunselected": onFeatureUnselect
  52.   });
  53.   map.addControl(select);
  54.   map.addControl(new OpenLayers.Control.MousePosition());
  55.   map.addControl(new OpenLayers.Control.LayerSwitcher());
  56.   select.activate();
  57.   map.zoomToMaxExtent();
  58. }
  59.  
  60. function changeOption(obj) {
  61.   var newBounds = obj.options[obj.selectedIndex].value.split(",");
  62.   var newExtent = new OpenLayers.Bounds(newBounds[0], newBounds[1], newBounds[2], newBounds[3]);
  63.   base.mergeNewParams({maxExtent: newExtent});
  64.   map.zoomToExtent(newExtent);
  65. }
  66.  
  67. function onPopupClose(evt) {
  68.   select.unselectAll();
  69. }
  70.  
  71. function onFeatureSelect(event) {
  72.   var feature = event.feature;
  73.   // Since KML is user-generated, do naive protection against Javascript.
  74.   var content = "<h2>"+feature.attributes.name + "</h2>" + feature.attributes.description;
  75.   if (content.search("<script") != -1) {
  76.     content = "Content contained Javascript! Escaped content below.<br />" + content.replace(/</g, "&lt;");
  77.   }
  78.   popup = new OpenLayers.Popup.FramedCloud("chicken",
  79.                        feature.geometry.getBounds().getCenterLonLat(),
  80.                        new OpenLayers.Size(100,100),
  81.                        content, null, true, onPopupClose);
  82.   feature.popup = popup;
  83.   map.addPopup(popup);
  84. }
  85.  
  86. function onFeatureUnselect(event) {
  87.   var feature = event.feature;
  88.   if(feature.popup) {
  89.     map.removePopup(feature.popup);
  90.     feature.popup.destroy();
  91.     delete feature.popup;
  92.   }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement