Close

August 3, 2009

KML Placemark Updates in Google Earth

While working on a prototyping project for a customer, I was having problems “updating” existing placemarks in Google Earth that had been created by a simple Network Link file. Granted I was writing a custom web server to server KML data, but it was not immediately clear why my KML data was not being interpreted correctly.  While Google Earth’s KML Documentation is fairly comprehensive, it seems to lack a good deal of simple examples.

The biggest issue I found, was trying to figure out how to update placemarks from one file, within another kml file. So we’ll start off with a basic InitFile.kml file like the following, which references a local host address (could be any webserver). This file is critical in allowing us to “update” objects (placemarks/etc) that exist within Google Earth.
[InitFile.kml]

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
 
<Folder>
 <name>Fun Folder</name>
<NetworkLink id="AA_LINK">
	<name>AA LINK</name>
	<Link>
		<refreshInterval>1</refreshInterval>
		<href>http://127.0.0.1/reloadObjects.kml</href>
	</Link>
</NetworkLink>
<NetworkLink id="AA_UPDATELINK">
	<name>AA UPDATE LINK</name>
	<Link>
		<refreshMode>onInterval</refreshMode>
		<refreshInterval>1</refreshInterval>
		<href>http://127.0.0.1/updateObjects.kml</href>
	</Link>
</NetworkLink>
</Folder>
</kml>

This file sets up two feeds in Google Earth. The first one, “http://127.0.0.1/loadObjects.kml”, loads all of your placemark data and objects.
The second feed is the “http://127.0.0.1/updateObjects.kml” file. This file contains the KML information to Update/Delete objects that were created in the first KML file. These files could also be served dynamically via any language (Php/Perl/Python etc).

Google Earth employs a security mechanism, whereas when you go to “Update” an object, it must already have been created by another network object.
For example, if the “updatesObject.kml” file were to update any objects loaded in the “loadObjects.kml” file, it MUST specify the correct “targetHref”. Since we would want to update the objects loaded in the initial “loadObjects.kml” file, the target would be “http://127.0.0.1/loadObjects.kml”.

Seems simple enough!

Also on a side-note, one thing that I had found was that the web server (if you are using a webserver to serve KML files), must respond with the exact server name that is specified in the Network link file. I had written my own simple web server, using basic TCP sockets, and had the initial response as listing some random server name. Google Earth refused to load any KML files from my custom webserver, until I had changed my server to respond as the exact address in the Network Link file (http://127.0.0.1).

Hope this helps!