Advertisement
Guest User

rdewit

a guest
Nov 4th, 2009
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <html>
  2.   <head>
  3.     <title>OpenLayers GetFeature Example</title>
  4.     <script src="../lib/OpenLayers.js"></script>
  5.     <link rel="stylesheet" href="../theme/default/style.css" type="text/css" />
  6.     <link rel="stylesheet" href="style.css" type="text/css" />
  7.     <style type="text/css">
  8.         ul, li { padding-left: 0px; margin-left: 0px; }
  9.     </style>
  10.   </head>
  11.  
  12.   <body>
  13.       <h1 id="title">GetFeature Example</h1>
  14.  
  15.       <div id="tags"></div>
  16.  
  17.       <p id="shortdesc">
  18.         Demonstrates sending a GetFeature query to an OWS. Returns information about a map feature in the side DIV.
  19.       </p>
  20.  
  21.     <a id="permalink" href="">Permalink</a><br />
  22.  
  23.     <div style="float:right;width:28%">
  24.         <h1 style="font-size:1.3em;">USA States</h1>
  25.         <p style="font-size:.8em;">Click a country to see statistics about the country below.</p>
  26.         <div id="nodeList">
  27.         </div>
  28.     </div>
  29.       <div id="map" class="smallmap"></div>
  30.  
  31.   <script defer="defer" type="text/javascript">
  32.     OpenLayers.ProxyHost = "proxy.cgi?url=";
  33.     var map = new OpenLayers.Map('map', {'maxResolution':'auto'});
  34.  
  35.     var wms = new OpenLayers.Layer.WMS( "OpenLayers WMS",
  36.         "http://localhost:18080/geoserver/ows", {'layers': 'topp:states'} );
  37.     map.addLayer(wms);
  38.     map.addControl(new OpenLayers.Control.Permalink('permalink'));
  39.     map.zoomToMaxExtent();
  40.     map.events.register('click', map, function (e) {
  41.             OpenLayers.Util.getElement('nodeList').innerHTML = "Loading... please wait...";
  42.  
  43.             var lonlat = map.getLonLatFromViewPortPx(e.xy);
  44.             var lonlatString = lonlat.toShortString().replace(/ /g, ''); // remove any spaces
  45.  
  46.             var filter = '<Filter xmlns="http://www.opengis.net/ogc" xmlns:gml="http://www.opengis.net/gml"><Intersects><PropertyName>the_geom</PropertyName><gml:Point srsName="EPSG:4326"><gml:coordinates>' + lonlatString + '</gml:coordinates></gml:Point></Intersects></Filter>';
  47.  
  48.             var params = {
  49.                 service: 'WFS',
  50.                 request: 'GetFeature',
  51.                 version: '1.1.0',
  52.                 typeName: wms.params.LAYERS,
  53.                 outputFormat: 'json',
  54.                 // remove the propertyName line below to get all attributes
  55.                 propertyName: 'STATE_ABBR,STATE_NAME,STATE_FIPS',
  56.                 filter: filter
  57.             };
  58.  
  59.             var u = OpenLayers.Util.removeTail(wms.url);
  60.             u += '?' + OpenLayers.Util.getParameterString(params);
  61.  
  62.             OpenLayers.loadURL(u, '', this, setHTML);
  63.             OpenLayers.Event.stop(e);
  64.       });
  65.     function setHTML(response) {
  66.         var jsonReader = new OpenLayers.Format.GeoJSON();
  67.         var features = jsonReader.read(response.responseText);
  68.         var html = '<hr />';
  69.         for (var i=0; i< features.length; i++) {
  70.             var feature = features[i];
  71.             html += '<strong>' + feature.id + '</strong>' + '<br />';;
  72.             for (var a in feature.attributes) {
  73.                 html += '<strong>' + a + '</strong>: ' + escape(feature.attributes[a]) + '<br />';
  74.             }
  75.         }
  76.         OpenLayers.Util.getElement('nodeList').innerHTML = html;
  77.     }
  78.   </script>
  79.  
  80.     <div id="docs">
  81.     </div>
  82.   </body>
  83. </html>
  84.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement