[OpenLayers-Users] how to add kml file and analyse with openlayers?

zhoujian_ab zhoujian_ab at 163.com
Thu Oct 8 21:14:51 EDT 2009


Hi:
   I also write the kml examples like sundial on openlayers web. I can,t achieve the same result to use geoserver's 'topp: states'. The following is the code, I don,t know what,s wrong. Thanks.

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <link rel="stylesheet" href="source/style1.css" type="text/css" />
    <link rel="stylesheet" href="source/style.css" type="text/css" />
    <style type="text/css">
        #map {
            width: 100%;
            height: 80%;
            border: 1px solid black;
        }
        .olPopup p { margin:0px; font-size: .9em;}
        .olPopup h2 { font-size:1.2em; }
    </style>
    <script src="OpenLayers/OpenLayers.js"></script>
    <script type="text/javascript">
        var lon = -100;
        var lat = 60;
        var zoom = 5;
        var map, select;
        function init(){
            map = new OpenLayers.Map('map');
            var wms = new OpenLayers.Layer.WMS(
                "OpenLayers WMS",
                "http://localhost:8081/geoserver/wms",
                {layers: 'topp:states'}
            );
         
          var sundials = new OpenLayers.Layer.Vector("KML",{
                projection: map.displayProjection,
                strategies: [new OpenLayers.Strategy.Fixed()],
                protocol: new OpenLayers.Protocol.HTTP({
                    url: "states.kml",
                    format: new OpenLayers.Format.KML({
                        extractStyles: true,
                        extractAttributes: true
                    })
                })
            });
            
            map.addLayers([wms, sundials]);
            
            select = new OpenLayers.Control.SelectFeature(sundials);
            
            sundials.events.on({
                "featureselected": onFeatureSelect,
                "featureunselected": onFeatureUnselect
            });
            map.addControl(select);
            select.activate();   
            map.zoomToExtent(new OpenLayers.Bounds(-127.61950064999999, 23.7351786,-64.08177035, 50.5925234000000056));
        }
        function onPopupClose(evt) {
            select.unselectAll();
        }
        function onFeatureSelect(event) {
            var feature = event.feature;
            // Since KML is user-generated, do naive protection against
            // Javascript.
            var content = "<h2>"+feature.attributes.name + "</h2>" + feature.attributes.description;
            if (content.search("<script") != -1) {
                content = "Content contained Javascript! Escaped content below.<br/>" + content.replace(/</g, "&lt;");
            }
            popup = new OpenLayers.Popup.FramedCloud("chicken", 
                                     feature.geometry.getBounds().getCenterLonLat(),
                                     new OpenLayers.Size(100,100),
                                     content,
                                     null, true, onPopupClose);
            feature.popup = popup;
            map.addPopup(popup);
        }
        function onFeatureUnselect(event) {
            var feature = event.feature;
            if(feature.popup) {
                map.removePopup(feature.popup);
                feature.popup.destroy();
                delete feature.popup;
            }
        }
    </script>
  </head>
  <body onload="init()">
      <h1 id="title">KML Layer Example</h1>
      <div id="tags"></div>
      <p id="shortdesc">
          Demonstrates loading and displaying a KML file on top of a basemap.
    </p>
    <div id="map" class="smallmap"></div>
    <div id="docs"></div>
  </body>
</html>

 I am making a digital map with Openlayers+geoserver+postgis. But It is my first time to use openlayers, I need some guides. I am Chinese, and my MSN is zhoujianab at hotmail.com. Thanks for your viewing, I need your help. If you can, please give me a way to contact, just like MSN, ICQ or QQ and so on. thank you. I am looking forward your reply. 

2009-10-09 



zhoujian_ab 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/openlayers-users/attachments/20091009/3903419b/attachment.html


More information about the Users mailing list