Iterating through the KML DOM...

238 views
Skip to first unread message

fraser

unread,
Jun 12, 2008, 4:47:59 AM6/12/08
to KML Developer Support - Google Earth Browser Plugin
Hi,

The plug-in is great, good work. I posted this elsewhere but figured
I'd start a topic here.
Basically, I have been playing around with various ways of adding
placemarks to
Google earth. At the moment I have a MySQL database and I am
generating KML files
from this via PHP. See: http://www.simpsonhaugh.co.uk/SimpsonHaugh/map/genMapKML.php

I am then loading the KML into the plug-in using fetchKml. I then
wanted to iterate through the KML file, get the placemarks data and
maybe build a sidebar,
etc. The thing is I just cant seem to get at any of the KmlPlacemarks
via:

ge.getFeatures().getChildNodes();

I can only get the KmlDocument node itself. I have set-up a
'handleClickPlacemark' function and this works fine, the code outputs
the type and name of the placemarks so I know they are loaded
correctly.

See: http://www.simpsonhaugh.co.uk/SimpsonHaugh/map/earth.php

How would one iterate through the rest of the KML to get at the
placemarks? Can this be done? I tried loading the KML via a
networklink with the same result...

Any help would be great!

Cheers,

Fraser.

marquies

unread,
Jun 12, 2008, 7:24:18 AM6/12/08
to KML Developer Support - Google Earth Browser Plugin
GetFeatures is a FeatureContainer, also Document or a Folder is a
FeatureContainer so you have to call getChildNodes() and iterate over
the Childs. Think that you have an xml structure.

regards
marquies
www.GONICUS.de

fraser

unread,
Jun 12, 2008, 8:17:07 AM6/12/08
to KML Developer Support - Google Earth Browser Plugin
Hey,

Thanks for the response...I realise its just an XML structure. Looking
at it now I see I was just missing a 'getFeatures()'

var features = ge.getFeatures().getChildNodes();
for(var i = 0; i < features.getLength(); i++)
{
var feat = features.item(i);
var name = feat.getName();
var type = feat.getType();
//
// var folders = feat.getChildNodes();
// error: "Object does not support this property or method"
//
var folders = feat.getFeatures().getChildNodes();
}

Doh!
> > Fraser.- Hide quoted text -
>
> - Show quoted text -

fraser

unread,
Jun 12, 2008, 2:44:20 PM6/12/08
to KML Developer Support - Google Earth Browser Plugin
For anyone that's interested here is a quick recursive function I put
together to iterate through the KML.
May not be exactly what you need but hope it gives people who are
stuck some ideas!

---
function traverseKml(node) {
alert(node.getType() + '; ' + node.getName());
if(node.getFeatures().hasChildNodes()) {
var subNodes = node.getFeatures().getChildNodes();
var length = subNodes.getLength();
for(var i = 0; i < length; i++) {
var eachSubNode = subNodes.item(i);
var nodeType = eachSubNode.getType();
var nodeName = eachSubNode.getName();
var nodeID = eachSubNode.getID();
switch(nodeType) {
case 'KmlFolder' :
traverseKml(eachSubNode);
break;
case 'KmlPlacemark' :
alert(nodeType + '; ' + nodeName + ' ' + nodeID);
break;
}
}
}
}
---
> > - Show quoted text -- Hide quoted text -
Reply all
Reply to author
Forward
Message has been deleted
Message has been deleted
0 new messages