[OpenLayers-Users] Parse and paste KML into an existing layer

Slawomir Messner slawomir.messner at staff.uni-marburg.de
Fri Jun 11 06:56:04 EDT 2010


  Hi,
I have a handler witch returns kml files with geometries, when I use it 
for a new layer all works fine and I see my geometries. But when I don't 
want a new layer I load the data with OpenLayers.loadURL, parse it with 
OpenLayers.Format.KML and add the resulting features then nothing shows. 
The layer has 16 but not visible.
The layer has projection 900913 so I transform them and when I want the 
parsed features in wkt the result is undefined.
Here is my code(when I remove the style and reprojection nothing changes):
penLayers.loadURL(myurl, myparam, this, this.parseData, this.onError);
parseData:function(req) {
              g = new OpenLayers.Format.KML({ extractStyles: true, 
extractAttributes: true });
             wkt = new OpenLayers.Format.WKT();
             features = g.read(req.responseText);
             var src = new OpenLayers.Projection('EPSG:4326');
             var dest = this.layer.projection;
             for (var feat in features) {
                 feature = features[feat];
                 if (feature != null && feature.geometry != null) {
                     if (feature.geometry instanceof 
OpenLayers.Geometry.Polygon) {
                         for (var i = 0; i < 
feature.geometry.components.length; i++) {
                             
OpenLayers.Projection.transform(feature.geometry.components[i], src, dest);
                         }
                     } else if (feature.geometry instanceof 
OpenLayers.Geometry.MultiPolygon) {
                         for (var i = 0; i < 
feature.geometry.components.length; i++) {
                             for (var j = 0; j < 
feature.geometry.components[i].components.length; j++) {
                                 
OpenLayers.Projection.transform(feature.geometry.components[i].components[j], 
src, dest);
                             }
                         }
                     } else if (feature.geometry instanceof 
OpenLayers.Geometry.Point) {
                         
OpenLayers.Projection.transform(feature.geometry, src, dest);
                     }
                     var styleMap = new OpenLayers.StyleMap();
                     OpenLayers.Util.extend(styleMap, 
this.layer.styleMap.styles["default"].defaultStyle);
                     feature.style = styleMap;
                 }
             }
             this.layer.addFeatures(features);
}
I noticed that some of the geomatry-attributes of the features are null, 
this is maybe the reason why I cannot see a feature on the map but why 
are they not parsed? When I use a new layer the features are on the map. 
The alternative code is :

                 //                var defaultStyle = new 
OpenLayers.Style({ 'pointRadius': 4, 'fillColor': '#123456', 
'fillOpacity': 0.4, 'stroke': true, 'strokeColor': '#654321', 
'strokeOpacity': 1, 'strokeWidth': 1, 'strokeDashstyle': 'solid' });
                 //                var selectStyle = new 
OpenLayers.Style({ 'pointRadius': 5, 'fillColor': '#FFFFFF', 
'fillOpacity': 0.6, 'stroke': true, 'strokeColor': '#FF2414', 
'strokeOpacity': 1, 'strokeWidth': 2, 'strokeDashstyle': 'solid' });
                 //                var layerStyleMap = new 
OpenLayers.StyleMap({ 'default': defaultStyle, 'select': selectStyle });
                 //                this.hostLayer = new 
OpenLayers.Layer.Vector("Statistik", {
                 //                    projection: new 
OpenLayers.Projection("EPSG:4326"),
                 //                    strategies: [new 
OpenLayers.Strategy.Fixed({ preload: true })],
                 //                    protocol: new 
OpenLayers.Protocol.HTTP({
                 //                        url: myurl,
                 //                        format: new 
OpenLayers.Format.REDEKML({
                 //                            extractStyles: true,
                 //                            extractAttributes: true
                 //                        })
                 //                    }), transitionEffect: "resize", 
visibility: true, styleMap: layerStyleMap, renderers: ["REDESVG", "SVG", 
"VML"]
                 //                });
                 //                this.map.addLayer(this.hostLayer);
Please help,
Slawomir



More information about the Users mailing list