przezdziecki / geoxml3

Automatically exported from code.google.com/p/geoxml3
0 stars 0 forks source link

KML Folders support #57

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
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 jrd...@gmail.com on 23 Mar 2012 at 11:18

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago
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 jrd...@gmail.com on 26 Mar 2012 at 7:12

GoogleCodeExporter commented 9 years ago
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 commented 9 years ago
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 jrd...@gmail.com on 2 Apr 2012 at 10:30

GoogleCodeExporter commented 9 years ago
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 commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
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 davej...@yahoo.com on 19 Jul 2014 at 8:10