Finding a placemark by ID

488 views
Skip to first unread message

TinyGrasshopper

unread,
Aug 11, 2008, 3:23:14 AM8/11/08
to KML Developer Support - Google Earth Browser Plugin
Hi Guys,

I've got a problem where I have the id of a placemark and want to find
the actual placemark object. Now i know i could walk the feature tree
to find it, but I was wondering if there's a quicker way?

eg: something like ge.getFeatureById(id)

Sorry if this has been covered elsewhere but I couldn't see any
existing answers looking through the headlines.

Cheers,
Chris

TinyGrasshopper

unread,
Aug 11, 2008, 9:01:01 PM8/11/08
to KML Developer Support - Google Earth Browser Plugin
So I ended up using this function if anyone's interested (it's pretty
simple but might save someone some time):

function getFeatureById(id, recursive, feature) {
var i;
var list;

if (typeof recursive == 'undefined') {
recursive = false;
}
if (typeof feature == 'undefined' ) {
feature = ge;
} else if (feature.getId() == id) {
return feature;
}
if ((feature.getType() == 'GEPlugin' || feature.getType() ==
'KmlFolder') && feature.getFeatures().hasChildNodes()) {
list = feature.getFeatures().getChildNodes();
for (i = 0; i < list.getLength(); i++) {
if (recursive) {
return getFeatureById(id, recursive, list.item(i) );
} else if (list.item(i).getId() == id) {
return list.item(i);
}
}
}
return null;
}

There's a few ways of calling it:

getFeatureById('hello')

will return the first feature with id = 'hello' in the top level of
the plugin's feature hierarchy.

getFeatureById('hello', true)

will return the first feature with id = 'hello' in the whole of the
plugin's feature hierarchy.

getFeatureById('hello', false, some_feature)

will return the first feature with id = 'hello' in the top level of
some_feature's feature hierarchy (assuming some_feature is a folder
that contains other features).

getFeatureById('hello', true, some_feature)

will return the first feature with id = 'hello' in the whole of
some_feature's feature hierarchy (assuming some_feature is a folder
that contains other features).

null is returned if no feature is found.

Cheers,
Chris

derek

unread,
Aug 14, 2008, 5:33:26 PM8/14/08
to KML Developer Support - Google Earth Browser Plugin
Hi Chris,

Thanks for giving us that code, but I have a question. Where and when
does the id of a KMLObject get set? In the documentation, it
indicates that there is a unique id for KML Object, but every time I
getID() in my code, I get an empty string. How have you added objects
to your plugin so that they have populated IDs?

Thanks!

Derek

TinyGrasshopper

unread,
Aug 14, 2008, 6:35:23 PM8/14/08
to KML Developer Support - Google Earth Browser Plugin
Hi Derek,

It's the argument to the "create..." functions on the GEPlugin class.

Eg:

function init() {
google.earth.createInstance("map3d", initCB, failureCB);
}

function initCB(object) {
ge = object;
ge.createPlacemark( < ID goes here > );
...
}

Cheers,
Chris

derek

unread,
Aug 15, 2008, 10:40:56 AM8/15/08
to KML Developer Support - Google Earth Browser Plugin
Well that was completely obvious and I'm embarrassed I didn't figure
that out! Thanks Chris!

- Derek

RTL

unread,
Aug 19, 2008, 12:53:27 PM8/19/08
to KML Developer Support - Google Earth Browser Plugin
Did you ever find an easier way to do this (something built-in) that
would do the search for you?

Thanks!

On Aug 11, 12:23 am, TinyGrasshopper wrote:
> Hi Guys,
>
> I've got a problem where I have theidof aplacemarkand want to find
> the actualplacemarkobject. Now i know i could walk the feature tree

TinyGrasshopper

unread,
Aug 19, 2008, 7:07:47 PM8/19/08
to KML Developer Support - Google Earth Browser Plugin
Nope :( still using the above function.

I've tried to keep the placemarks that i look up with it on the top
level of the feature tree so that there's not too much to search. It
seems pretty fast - I use it during global mouse move events so it'd
be obvious if it were a bottleneck.

Cheers,
Chris

TinyGrasshopper

unread,
Aug 19, 2008, 7:20:35 PM8/19/08
to KML Developer Support - Google Earth Browser Plugin

TinyGrasshopper

unread,
Aug 21, 2008, 9:59:33 PM8/21/08
to KML Developer Support - Google Earth Browser Plugin
Oops, noticed a bug. Also I decided to do it properly and always
require a feature (or the plugiin to be passed in) rather than looking
at a global.

Here's a working version:

function getFeatureById(feature, id, recursive) {
var i;
var list;
var geom;

if (typeof recursive == 'undefined') {
recursive = false;
}
if (feature.getType() != 'GEPlugin' && feature.getId() == id) {
return feature;
}

if ((feature.getType() == 'GEPlugin' || feature.getType() ==
'KmlFolder') && feature.getFeatures().hasChildNodes()) {
list = feature.getFeatures().getChildNodes();
for (i = 0; i < list.getLength(); i++) {
if (recursive) {
sub_feature = getFeatureById(list.item(i), id, recursive);
if (sub_feature != null) return sub_feature;

} else if (list.item(i).getId() == id) {
return list.item(i);
}
}
}
return null;
}




TinyGrasshopper

unread,
Aug 21, 2008, 10:00:35 PM8/21/08
to KML Developer Support - Google Earth Browser Plugin
Also, here's a version that returns features or geometries:

function getGeometryById(geometry, id) {

var geom;
var list;
var i;

if (geometry.getId() == id) return geometry;

if (geometry.getType() == "KmlMultiGeometry") {
if (geometry.getGeometries().hasChildNodes()) {
list = geometry.getGeometries().getChildNodes();
for (i = 0; i < list.getLength(); i++) {
geom = list.item(i);
geom = getGeometryById(geom, id);
if (geom != null) return geom;
}
}
} else if (geometry.getType() == "KmlPolygon") {
if (geometry.getOuterBoundary().getId() == id) return
geometry.getOuterBoundary();

if (geometry.getInnerBoundaries().hasChildNodes()) {
list = geometry.getInnerBoundaries().getChildNodes();
for (i = 0; i < list.getLength(); i++) {
geom = list.item(i);
geom = getGeometryById(geom, id);
if (geom != null) return geom;
}
}
}
return null;
}

function getItemById(feature, id) {
var i;
var list;
var geom;

if (feature.getType() != "GEPlugin") {
if (feature.getId() == id) return feature;
if (feature.getType() != "KmlFolder") {
geom = getGeometryById(feature.getGeometry(), id);
if (geom != null) return geom;
}
}

if ((feature.getType() == 'GEPlugin' || feature.getType() ==
'KmlFolder') && feature.getFeatures().hasChildNodes()) {
list = feature.getFeatures().getChildNodes();
for (i = 0; i < list.getLength(); i++) {
sub_feature = getItemById(list.item(i), id);
if (sub_feature != null) return sub_feature;
}
}
return null;

TinyGrasshopper

unread,
Aug 21, 2008, 10:23:36 PM8/21/08
to KML Developer Support - Google Earth Browser Plugin
Sigh, more bugs, more new versions:

/**
* Search a geometry hierarchy for a geometry with the given id.
*
* @param geometry The geometry to start the search from.
* @param id The id of the geometry to find.
* @return The found geometry or null if not found.
*/
function getGeometryById(geometry, id) {

var geom;
var list;
var i;

if (geometry.getId() == id) return geometry;

if (geometry.getType() == "KmlMultiGeometry") {
if (geometry.getGeometries().hasChildNodes()) {
list = geometry.getGeometries().getChildNodes();
for (i = 0; i < list.getLength(); i++) {
geom = list.item(i);
geom = getGeometryById(geom, id);
if (geom != null) return geom;
}
}
} else if (geometry.getType() == "KmlPolygon") {
if (geometry.getOuterBoundary().getId() == id) return
geometry.getOuterBoundary();

if (geometry.getInnerBoundaries().hasChildNodes()) {
list = geometry.getInnerBoundaries().getChildNodes();
for (i = 0; i < list.getLength(); i++) {
geom = list.item(i);
geom = getGeometryById(geom, id);
if (geom != null) return geom;
}
}
}
return null;
}


/**
* Search a feature hierarchy for a feature or geometry with the given
id.
*
* @param feature The toplevel feature (or a GEPlugin) to search.
* @param id The id of the feature or geometry to find.
* @return The found feature or geometry or null if not found.
*/
function getItemById(feature, id) {
var i;
var list;
var geom;
var sub_feature;

if (feature.getType() != "GEPlugin") {
if (feature.getId() == id) return feature;
if (feature.getType() != "KmlFolder") {
geom = getGeometryById(feature.getGeometry(), id);
if (geom != null) return geom;
}
}

if ((feature.getType() == "GEPlugin" || feature.getType() ==
"KmlFolder" || feature.getType() == "KmlDocument") &&
feature.getFeatures().hasChildNodes()) {
list = feature.getFeatures().getChildNodes();
for (i = 0; i < list.getLength(); i++) {
sub_feature = getItemById(list.item(i), id);
if (sub_feature != null) return sub_feature;
}
}
return null;
}


/**
* Search a feature hierarchy for a feature with the given id.
*
* @param feature The toplevel feature (or a GEPlugin) to search.
* @param id The id of the feature to find.
* @param recursive true = search the whole hierarchy,
* false = search just the children of the given feature.
* @return The found feature or null if not found.
*/
function getFeatureById(feature, id, recursive) {
var i;
var list;
var sub_feature;

if (typeof recursive == 'undefined') {
recursive = false;
}
if (feature.getType() != 'GEPlugin' && feature.getId() == id) {
return feature;
}

if ((feature.getType() == 'GEPlugin' || feature.getType() ==
'KmlFolder' || feature.getType() == 'KmlDocument') &&

RTL

unread,
Aug 25, 2008, 3:15:21 PM8/25/08
to KML Developer Support - Google Earth Browser Plugin
Thank you!

rtl
> > > > > > > Chris- Hide quoted text -
>
> - Show quoted text -
Reply all
Reply to author
Forward
0 new messages