Search code examples
javascriptgoogle-maps-markersgoogle-earth-plugin

Javascript for adding/removing placemarks to Google Earth plug-in map


Does anyone know if there is a way to assign a unique ID to placemarks when you create them and add them to a Google earth map via javascript? Here is an example of the kind of code I'm using to create and add placemarks:

    var icon = ge.createIcon('');
    icon.setHref('<url here>');
    var style = ge.createStyle('');
    style.getIconStyle().setIcon(icon);
    style.getIconStyle().setScale(0.65);

    var pm = ge.createPlacemark('');
    pm.setStyleSelector(style);
    pm.setName("Type1");  // <-- NEED ANOTHER METHOD (ex. pm.SetId('uniqueId'))

    var pmPoint = ge.createPoint('');
    pmPoint.setLatitude(35.859545);
    pmPoint.setLongitude(-92.388783);

    pm.setGeometry(pmPoint);
    ge.getFeatures().appendChild(pm);

-------------------------------------------------------------

I have multiple sets of KmlPlacemarks I want to add to the map, and I don't want to have to assign a unique name to each in order to be able to remove them (mainly because I don't want the name to show up on the map). I'm trying to remove certain placemarks using the following (incomplete) block of code:

var children = ge.getFeatures().getChildNodes();
for(var i = 0; i < children.getLength(); i++) { 
    var child = children.item(i);
    if(child.getType() == 'KmlPlacemark') {
        if(... ??? ...) {  **// <-- don't want to use if(child.getName().indexOf('criteria') !== -1)**
            ge.getFeatures().removeChild(child);
        }
    }
}

-------------------------------------------------------------

Does someone know of another way to accomplish this? I tried using child.getUrl() but that doesn't return anything that I could use to identify which KmlPlacemarks I want to remove from the map...

Or maybe there is a way to set the name visibility on the map to false?

Thanks in advance.

Brandon


Solution

  • When you create a placemark

       var pm = ge.createPlacemark('');
    

    Use this to set it's 'id'

       var pm = ge.createPlacemark('uniqueID');
    

    then when you wish to remove it

       if(child.getType() == 'KmlPlacemark') {
        if(... ??? ...) {  **// <-- don't want to use if(child.getName().indexOf('criteria') !== -1)**
            ge.getFeatures().removeChild(child);
        }
    }
    

    becomes

       if(child.getType() == 'KmlPlacemark') {
        if(child.getId()=='uniqueID')
            ge.getFeatures().removeChild(child);
        }
    }