Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KML Folders support #57

Open
GoogleCodeExporter opened this issue Apr 8, 2015 · 8 comments
Open

KML Folders support #57

GoogleCodeExporter opened this issue Apr 8, 2015 · 8 comments

Comments

@GoogleCodeExporter
Copy link

I've found that the class doesn't support folders into KMLs.

I've done a little patch to add support for this element. I've done the 
modifications over the KMZ branch becouse I need polygons support, but I 
suspect that can be applied easily to the other branches. At least with all the 
KMLs I have tried, works transparently and considers folders as placemarks upon 
load and process.

Just replace the line:
      (515ish) var placemarkNodes = getElementsByTagName(responseXML, 'Placemark');

With the block:
      /////////////////////////////////////////////////////////////////////////
      // JRD + 2012-03-23 - Folders acting as placemarks for compatibility
      //var placemarkNodes = getElementsByTagName(responseXML, 'Placemark');
      var placemarkNodes = [];
      var folders = getElementsByTagName(responseXML, 'Folder');
      if( folders && folders.length )
      {
        for(var f = 0; f < folders.length; f++ )
        {
            var folder = folders[f].childNodes;
            for( var i = 0; i < folder.length; i++ )
            {
                // console.log( typeof( folder[i] ), folder[i].nodeName, folder[i], folder[i].constructor.name );
                if( folder[i].constructor.name === 'Element' && folder[i].nodeName != 'name' )
                {
                    // Is a folder. Push it as a placemark
                    placemarkNodes.push( folder[i] );
                }
            }
        }
      }
      else
        placemarkNodes = getElementsByTagName(responseXML, 'Placemark');
      // JRD - End of modification
      /////////////////////////////////////////////////////////////////////////

This patch is a first approximation and I will continue with the tests, but can 
be a solution for the ones who need folders support.

Hope it helps and thanks for the hard work.


Original issue reported on code.google.com by [email protected] on 23 Mar 2012 at 11:18

@GoogleCodeExporter
Copy link
Author

Have you looked at geoxml-v3?

http://code.google.com/p/geoxml-v3/

Lance has much better sidebar support in that (which is a port of his v2 
version which is much more full featured)

I wasn't planning on doing any of that in geoxml3 itself.

Original comment by geocodezip on 23 Mar 2012 at 3:10

  • Added labels: Priority-Low, Type-Enhancement
  • Removed labels: Priority-Medium, Type-Defect

@GoogleCodeExporter
Copy link
Author

Hi, didn't knew about the v3 library! thanks a lot!!!. I'll study it thoroughly 
;)

The code I wrote it's not refered to sidebar support... it's only a patch to 
allow loading of KMLs with <folder>...</folder> structures.

BTW. A little update to make it work on iPad/iPhone. Replace the "if" with this 
one:

if( folder[i].nodeType == 1 && folder[i].nodeName != 'name' ) // && 
folder[i].constructor.name === 'Element'

Original comment by [email protected] on 26 Mar 2012 at 7:12

@GoogleCodeExporter
Copy link
Author

Do you have an example KML file that contains folders and doesn't work without 
your change?  And/or a link to a map that shows the issue?

Original comment by geocodezip on 26 Mar 2012 at 12:04

@GoogleCodeExporter
Copy link
Author

The only thing I do to reproduce the problem is to open a KML in Google Earth 
(appears in a folder) and save it clicking on the floder it creates.

If you can't reproduce it, tell me and I'll look for a good example.

Original comment by [email protected] on 2 Apr 2012 at 10:30

@GoogleCodeExporter
Copy link
Author

I don't use Google Earth and I'm not sure how that applies to geoxml3.

Original comment by geocodezip on 2 Apr 2012 at 12:40

@GoogleCodeExporter
Copy link
Author

[deleted comment]

1 similar comment
@GoogleCodeExporter
Copy link
Author

[deleted comment]

@GoogleCodeExporter
Copy link
Author

I am looking into your code as well and was interested in a sidebar. The 
feature that is missing is that of Folder name, but since that is really a 
parent of the Placemark I modified your js to include an array of parents to 
each Placemark. This will allow grouping of the Placemarks into the logical 
groups that may or may not be present in a KML. NOTE: if the KML doesn't have 
Folder tags then the search for parent nodes will always only show one parent 
(the first 'name' tag). Change is 8 lines of code.

//code show here
//the baseUrl is the first 'name' tag in the KML document
var baseUrl = geoXML3.nodeValue(responseXML.getElementsByTagName('name')[0]);

//start of your code
var placemarkNodes = responseXML.getElementsByTagName('Placemark');

      for (pm = 0; pm < placemarkNodes.length; pm++) {
        // Init the placemark object
        node = placemarkNodes[pm];

//parent, grandparent etc root is always the baseUrl name
//get nodes parent
var pNode = node.parentNode;
//initialize parent array
var parent = [];
//add parent 'name' to array
parent[0] = geoXML3.nodeValue(pNode.getElementsByTagName('name')[0]);

//while first item in parent array isn't baseUrl continue
while (parent[0] !== baseUrlNode) {
  //each time through get the parent of the parent
  pNode  = pNode.parentNode;

  //add the parent 'name' to the front of the array            parent.unshift(geoXML3.nodeValue(pNode.getElementsByTagName('name')[0]));
} //end search for nodes lineage

//your code again
//except new item in placemark of the above parent array
        placemark = {
          name:  geoXML3.nodeValue(node.getElementsByTagName('name')[0]),
      parent: parent,
          description: geoXML3.nodeValue(node.getElementsByTagName('description')[0]),
          styleUrl: geoXML3.nodeValue(node.getElementsByTagName('styleUrl')[0])
        };

Original comment by [email protected] on 19 Jul 2014 at 8:10

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant