2

I have just got a problem displaying extended data from a KML file in OpenLayers 2. I hope you can help me with this issue. I have already searched for a solution, but I do not find a real solution for it. I have to mention that I am not a programmer and just modify free available scripts and codes.

I have a standard KML file like:

<?xml version="1.0" encoding="utf-8" ?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document id="root_doc">
<Style id="style1">
    <IconStyle>
      <Icon>
        <href>pr.png</href>
      </Icon>
    </IconStyle>
  </Style>
<Schema name="pr14" id="pr14">
    <SimpleField name="Description" type="string"></SimpleField>
    <SimpleField name="Nr." type="int"></SimpleField>
    <SimpleField name="Landkreis" type="string"></SimpleField>
    <SimpleField name="Lage (Koordinate X)" type="float"></SimpleField>
    <SimpleField name="Lage (Koordinate Y)" type="float"></SimpleField>
    <SimpleField name="Kapazität" type="string"></SimpleField>
</Schema>
<Folder><name>P+R pr14</name>
  <Placemark>
    <name>P+R Altenbach</name>
    <description>8011018</description>
    <ExtendedData><SchemaData schemaUrl="#pr14">
        <SimpleData name="Nr.">1</SimpleData>
        <SimpleData name="Landkreis">LL</SimpleData>
        <SimpleData name="Lage (Koordinate X)">51.356068</SimpleData>
        <SimpleData name="Lage (Koordinate Y)">12.681333</SimpleData>
        <SimpleData name="Kapazität">0</SimpleData>
    </SchemaData></ExtendedData>
   <styleUrl>#style1</styleUrl> 
      <Point><coordinates>12.681333,51.356068</coordinates></Point>
  </Placemark>

</Folder>
</Document></kml>

Now, I would like to display beside of the "name" and "description" field which work fine also the attributes from Extended Data fields.

 var kmllayer2 = new OpenLayers.Layer.Vector("P+R Pl&auml;tze", {
                maxResolution: map.getResolutionForZoom(9),
            strategies: [new OpenLayers.Strategy.Fixed()],
            protocol: new OpenLayers.Protocol.HTTP({
                url: "pr14_1.kml",
                format: new OpenLayers.Format.KML({
                    extractStyles: true,
                    extractAttributes: true,
                    maxDepth: 2

To display the name in my popup I use '+feature.attributes.name+', but I cannot sort out how to modify this to get also the extended data values like 'Kapazität" for example. enter image description here

var controls2 = {
          selector2: new OpenLayers.Control.SelectFeature(kmllayer2, { onSelect: createPopup2, onUnselect: destroyPopup2 })
        };

        function createPopup2(feature) {
          feature.popup = new OpenLayers.Popup.FramedCloud("pop",
              feature.geometry.getBounds().getCenterLonLat(),
              null,
              '<div class="markerContent"><h3><font color="004A84">'+feature.attributes.name+'</font></h3></div>',
              null,
              true,
              function() { controls2['selector2'].unselectAll(); }
          );

          //feature.popup.closeOnMove = true;
          map.addPopup(feature.popup);
        }

        function destroyPopup2(feature) {
          feature.popup.destroy();
          feature.popup = null;
        }

         map.addControl(new OpenLayers.Control.LayerSwitcher({'ascending':false}));


        map.addControl(controls2['selector2']);
        controls2['selector2'].activate();

Could you give me a hint?

1

The solution for it is:

+feature.attributes."SimpleData name".value+

Example:

+feature.attributes.Kapazität.value+

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.