Search code examples
google-maps-api-3kml

Entity replacement not working in a KML/XML file, how do I use this data?


basically I want to put information into a balloon in Maps API, this is the KML file, the data is stored using SimpleData tags, and I am trying to access to it from the BalloonStyle text tag.

But it doesn't work, in the baloon is displayed simply $[something]. After some research, I discovered Entity replacement may not be supported anymore by SimpleData tags.

So how do I manage the data? I got the data from ogr2ogr conversion from a shapefile and I don't know how to manage its output to make it use ExtendedData and Data tags.

Thank for your help.


Solution

  • You can replace <SchemaData><SimpleData> with <Data><value> elements with a text editor preferably one that can perform regular expression replacements on searches such as NotePad++.

    You start with this:

    <ExtendedData>
      <SchemaData schemaUrl="#biblioteche">
            <SimpleData name="INDIRIZZO">VIA SAN VITTORE, 21</SimpleData>
            <SimpleData name="TIPOLOGIA">BIBLIOTECHE</SimpleData>
            ...
            <SimpleData name="ID">0</SimpleData>
      </SchemaData>
    </ExtendedData>
    

    And need to convert to this form:

    <ExtendedData> 
        <Data name="INDIRIZZO">
            <value>VIA SAN VITTORE, 21</value>
        </Data>
        <Data name="TIPOLOGIA">
            <value>BIBLIOTECHE</value>
        </Data>
            ...
        <Data name="ID">
            <value>0</value>
        </Data>
    </ExtendedData>
    

    Globally make the following replacements (in this order):

    # Find what Replace with
    1. <SchemaData schemaUrl="#biblioteche">
    2. </SchemaData>
    3. <SimpleData <Data
    4. (<Data name=".*?">) \1<value>
    5. </SimpleData> </value></Data>

    Steps 1 and 2 have an empty target such that you delete the element.
    Step 4 is the only step that needs to be done as a regular expression.

    working example