KML file not working? - kml

If I double Click on my KML file it does what it should and start GE and does as expected. When I put it in to my HTML file it will not work. I then put it into http://earth-api-samples.googlecode.com/svn/trunk/examples/kml-fetch-interactive.html and it doesn't work from there either. This is the link to the KLM https://dl.dropbox.com/u/61240296/myPoints.Kml. It is bascially a direct copy of a Google Sample. Are KML files dependent on other KML files? One thing I have notice is there are no roads so it appears as though non of the KML files are open or active.
<?xml version="1.0" encoding="UTF-8"?>
xmlns:gx="http://www.google.com/kml/ext/2.2"
xmlns:kml="http://www.opengis.net/kml/2.2"
xmlns:atom="http://www.w3.org/2005/Atom">
<Camera>
<longitude>-93.2539393007755</longitude>
<latitude>45.5456585437059</latitude>
<altitude>139.629438</altitude>
<heading>-70.0</heading>
<tilt>75</tilt>
</Camera>
<Placemark>
<name>Placemark from KML file</name>
<Point>
<coordinates>-93.2539393007755, 45.5456585437059</coordinates>
</Point>
</Placemark>
</Document>
</kml>

Your KML file is fine. It also works perfectly in the example page you link to. The problem is the sample page does not change the view after loading your kml file. It doesn't even try to.
Edit this function
function finishFetchKml(kmlObject) {
// check if the KML was fetched properly
if (kmlObject) {
// add the fetched KML to Earth
currentKmlObject = kmlObject;
ge.getFeatures().appendChild(currentKmlObject);
} else {
// wrap alerts in API callbacks and event handlers
// in a setTimeout to prevent deadlock in some browsers
setTimeout(function() {
alert('Bad or null KML.');
}, 0);
}
}
to look like this
function finishFetchKml(kmlObject) {
// check if the KML was fetched properly
if (kmlObject) {
// add the fetched KML to Earth
currentKmlObject = kmlObject;
ge.getFeatures().appendChild(currentKmlObject);
///////////////////////////////////////////////
// this is what you need to add
var myView = currentKmlObject.getAbstractView();
ge.getView().setAbstractView(myView);
//////////////////////////////////////////////
} else {
// wrap alerts in API callbacks and event handlers
// in a setTimeout to prevent deadlock in some browsers
setTimeout(function() {
alert('Bad or null KML.');
}, 0);
}
}
Then you need to decide if you want to assign that <Camera> view to the <Placemark> or the <Document>
and in case you didn't realise, your <Camera> view does NOT look at the <Placemark> it looks at the horizon from a spot directly above the <Placemark>

If you are trying to load a well formed KML file and the file doesn't load properly you should see the headers of the web server using the developer tools of chrome, firebug or similar.
If you are using apache as your webserver you should add to your httpd.conf:
AddType application/vnd.google-earth.kml+xml .kml
AddType application/vnd.google-earth.kmz .kmz

Related

kml document auto refresh in google earth

I have an app that over time writes Placemarks to a kml file, slowly building up a route over time.
When I open the file in Google Earth, I can see the Placemarks already in the file, but any new ones added to the doc aren't shown on the map, until I reopen the document again.
Is there any way of getting the document to auto refresh in Google earth?
I've tried putting a NetworkLink into the document whose href points itself (the file) with a refreshMode of onChange, and that works, but the whole document is reloaded on each change and each point is displayed again, which is messy.
NOTE: I can't change the apps code, to generate single Placemark updates, which I believe is the proper way of using NetworkLinks.
The only way to perform incremental updates in KML is using the NetworkLinkControl element in conjunction with a NetworkLink.
NetworkLinkControl controls the behavior of files fetched by a <NetworkLink> in which you can change, add, or delete elements that you already fetched.
<NetworkLinkControl>
<cookie>cookie=sometext</cookie>
<linkName>link name</linkName>
<Update>
<targetHref>same-targethref-for-networklink</targetHref>
<Create>
<Document targetId="targetdoc">
<Placemark>
<name>Created place1</name>
</Placemark>
<Placemark>
<name>Created place2</name>
</Placemark>
</Document>
</Create>
...
</Update>
</NetworkLinkControl>
The NetworkLinkControl is tricky to get working such as the URLs in the NetworkLink and NetworkLinkControl must match exactly.
You can find a tutorial with an example to get started.
https://developers.google.com/kml/documentation/updates
So I guess you can't. I thought there might be some kind of structure within kml to achieve this, nut it would appear not.

Google Earth KML - Weird GE behavior when using balloon links to fragment URLs when doc loaded via NetworkLink

KML newb here. First post.
Using GE client application, v7.12. Have fairly simple KML doc containing placemarks to several GPS time-based breadcrumb points. Wrote a simple C# app to generate the kml doc. Expect to have several more like files for additional tracked entities. I get this working and decide to fancy up the placemark balloons via HTML. Ultimately decide I'd like to have links in the balloon to move to the previous or next placemark. Find advice on this site and make it happen.
Since my app may update(by rewriting) the doc at times it would be nice to not have to manually reload it. Plus there will ultimately be other docs for other tracked entities, all controlled/updated by my app. OK - NetworkLink to the rescue. Cool feature and gives me the option of dynamically modifying the networklinks file to only include the entity doc files that have changed. Plus big bonus -- turning 'flyToView' off so that when networklinks file is reloaded or refresh fires, I'm not pulled away from whatever I'm currently viewing by the default 'LookAt' that normally seems to happen when a doc is loaded directly (anyone know a way around this?)
Very happy, until I discover that when I click one of my balloon links to move to the next or previous point, GE decides to create a new copy of the entire containing document under Temporary Places and now I have two entire sets of points displayed, one hiding the other. On the surface, my next/prev links appear to be working, but they are actually only working with the newly created doc. To test, I alter the original doc with an entirely different set of locations. Reload the networklinks doc to bring in the new point-laden doc and see the new set of points. But when I use any balloon links on the new, they jump to points on the older doc that GE had created on its own. Delete the GE-created version, click balloon and viola, GE creates a new copy of it and were back where we started. Frustrating.
As placemark IDs, I'm using strings like "TPn", where n increments. In my balloon links, I'm linking to the fragment URLs such as href="#TPn-1" and href="TPn+1", ie TP2 links to TP1 and TP3, etc, and I account for both placemarks on the end so they don't have bad links to non-existent IDs. BTW, I've found when you do activate a link to a non-existent fragment URL, GE also decides to create a copy of the file, even if the file was loaded directly and not via a networklink, so you again have one point set laying on top of the other. However, I'm confident I have no bad links in this case.
Any light that can be shed would be appreciated. Or alternate ways of achieving my goals. I'm a KML newb after all and maybe there's something I'm doing wrong.
I suppose if I have to, I will sacrifice the balloon links. Being able to reload only one networklinks document for a collection of files + timed refresh + turning off flyToView are too desirable to lose now.
Thanks!
EDIT: Additional info - The duplicate document is created only when using balloon links. Clicking a link in the treeview under the placemark does not cause this anomaly. (In the code sample, remove the null Snippet element for each Placemark to see this effect.) Furthermore, it appears that a balloon link to ANY fragment URL causes the anomaly, such as a link back to a containing folder or document.
EDIT: Added 2 code samples. These are pared down to my essential needs, but still demonstrate the problem. Interesting to note the difference in the GE tree view when the data file 'document' element has an 'id' defined vs not, but it has no bearing on the problem. I prefer the look when the id is omitted.
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!-- File is: C:\myTwoDataPoints.kml -->
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document id="myDoc">
<name>Two Points Doc Name Here</name>
<description>Doc Description Here</description>
<visibility>0</visibility>
<Snippet>Contains Two Data Points</Snippet>
<Style id="myStyle">
<BalloonStyle >
<text><![CDATA[Link To: $[description] ]]></text>
</BalloonStyle>
</Style>
<Folder>
<name>Points Folder</name>
<open>1</open>
<Placemark id="TP1">
<name>Miami, FL, USA</name>
<Snippet />
<description><![CDATA[New York]]></description>
<styleUrl>#myStyle</styleUrl>
<Point>
<coordinates>-80.226439,25.788969,0</coordinates>
</Point>
</Placemark>
<Placemark id="TP2">
<name>New York, NY, USA</name>
<Snippet />
<description><![CDATA[Miami]]></description>
<styleUrl>#myStyle</styleUrl>
<Point>
<coordinates>-74.005973,40.714353,0</coordinates>
</Point>
</Placemark>
</Folder>
</Document>
</kml>
and...
<?xml version="1.0" encoding="UTF-8"?>
<!-- File is: C:\myNetworkLink.kml -->
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<name>NetworkLink Document Name Here</name>
<visibility>0</visibility>
<open>1</open>
<NetworkLink>
<name>NetworkLink Name Here</name>
<refreshVisibility>1</refreshVisibility>
<flyToView>0</flyToView>
<Link><href>file:///C:/myTwoDataPoints.kml</href></Link>
</NetworkLink>
</Document>
</kml>

Set Initial View When Opening KMZ in GE

I am creating a KMZ file as output from my program (using SharpKML). How can I set the initial view so that when this file is opened in Google Earth it centres and zooms around my data?
When I open a KML file in GE this behaviour is by default, but when I open my KMZ the browser just opens to the default whole world view centred on users country. If I double click the main KML within my KMZ file, then I get the correct zoom and centering around my data. How can I make this happen automatically when Google Earth loads the KMZ?
Thanks.
If you add a LookAt or Camera to the first element in your root KML file (first .kml file within your KMZ) Google Earth will start at that location when that file is loaded.
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2">
<Placemark>
<name>Google office</name>
<LookAt>
<longitude>-122.087387</longitude>
<latitude>37.422130</latitude>
<altitude>0</altitude>
<heading>-0.23</heading>
<tilt>47.81</tilt>
<range>115.5</range>
<gx:altitudeMode>relativeToSeaFloor</gx:altitudeMode>
</LookAt>
<Point>
<coordinates>-122.087461,37.422069</coordinates>
</Point>
</Placemark>
</kml>
If you have multiple features then add the <LookAt> to the root-level Document.
<kml>
<Document>
<LookAt>
...
</LookAt>
<Placemark>
</Placemark>
...
There is also a really easy way to do this in Google Earth/Google Earth pro:
Simply right click and bring up properties of the Folder in Places.
Go to the view tab and click the Snapshot current view.
Then save place as to update the kmz/kml.
This updates the <LookAt> at the level you click on- so for a point at the Placemark level or you can do it at the folder level.
I used to open my kmz files in Javascript and they would center automatically, but now as of a few weeks ago, my maps open up above the north pole. I created the kmz files so long ago I don't want to look at them or edit them. Is there some code I can add so that it works like it did before? I had before my open code
var overlay = new GGeoXml(\"http://www.mysite.com/data/file.kmz" ) ;
map.addOverlay( overlay ) ;
this bogus looking code that does not seem to initialze anything, but worked
var bounds = new GLatLngBounds();
// initialize bounds ??
var SWcorner= new GLatLng(parseFloat(87),parseFloat(-1));
var NEcorner= new GLatLng(parseFloat(89),parseFloat(1));
bounds.extend(SWcorner);
bounds.extend(NEcorner);
map.setCenter(new GLatLng(88.0, 0), map.getBoundsZoomLevel(bounds),
G_PHYSICAL_MAP);

What is an API object for the GE method FlyToObject?

In the GE Plugin API GEHelper.cs there is a function FlyToObject. Since I've been unable to get the kml to fly me to a placemark I thought I would use the API call.
The required arguments for FlyToObject are:
"dynamic ge,"
"dynamic feature,"
I'm stuck on what the 'feature' argument is. The description says "the api object". I'm not sure which api object that is referring to or how to create it.
What I'm trying to do is automatically (upon loading the file) "fly to" a placemark in my kml file.
If anyone knows how to do what I'm asking with KML that would be great. Here is the KML file I'm loading that does NOT work. This is one of Google's example files.
<kml xmlns="http://www.opengis.net/kml/2.2">
<Placemark>
<name>300m straight down</name>
<Camera>
<longitude>-122.4783</longitude>
<latitude>37.812</latitude>
<altitude>300</altitude>
<heading>0</heading>
<tilt>0</tilt>
<roll>0</roll>
<altitudeMode>absolute</altitudeMode>
</Camera>
</Placemark>
</kml>
THANKS!
If you are loading Google Earth into your own webpage, you have access to the GE API via javascript. Therefore if you want to set a certain view, use the following code
// Create a new LookAt.
var lookAt = ge.createLookAt('');
// Set the position values.
lookAt.setLatitude(36.584207);
lookAt.setLongitude(-121.754322);
lookAt.setRange(5000.0); //default is 0.0
// Update the view in Google Earth.
ge.getView().setAbstractView(lookAt);
More advanced: If you are loading a custom kml file, with your own Placemarks. Assign them as id as such in your kml file
<Placemark id="uniqueId">
Then, once loaded, use this code
placemark = ge.getElementById(uniqueId);
if (placemark == null) {
return false;
} else if (placemark.getAbstractView()) {
ge.getView().setAbstractView(lastViewLoaded);
}

How to properly update Google Earth KML using NetworkLinkControl and the Java API for KML (JAK)?

I'm building an application that serves up data to a standalone Google Earth client. I want to send over an initial set of data, then update it dynamically using <NetworkLinkControl> and <Update><cookie> tags as things change on the server. I'm generating the KML using the Java API for KML (JAK) library. Unfortunately, while I can confirm that GE is refreshing my NetworkLink and pulling down the Updates I'm sending, none of my updates are showing up in GE. After lots of reading, it seems like it might be that the Update's <targetHref> might be the issue, but I'm 99.9% sure I'm sending over the same string.
Part of what has me confused is that I've seen conflicting info on whether or not the <cookie> element's value needs to be appended to <targetHref>. I did actually see one early prototype updating when I was serving up hand-written test KML files from a static server URL, so I suspect it's not. Actually, that's what's frustrating at the moment: I've seen updating work on my own machine, but can't get it working now with what looks like valid and correct KML.
The current setup looks like this (extraneous XML namespaces stripped for clarity; "$CLIENT_ID" is a GUID-like string):
Root KML file served from http://server/kml/${CLIENT_ID}:
<kml xmlns="http://www.opengis.net/kml/2.2"
xmlns:gx="http://www.google.com/kml/ext/2.2"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:xal="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0"><NetworkLink>
<Link>
<href>http://server/kmlupdates/${CLIENT_ID}</href>
<refreshMode>onInterval</refreshMode>
<refreshInterval>1.0</refreshInterval>
<viewRefreshTime>0.0</viewRefreshTime>
<viewBoundScale>0.0</viewBoundScale>
</Link>
</NetworkLink></kml>
Initial content KML served from http://server/kmlupdates/${CLIENT_ID}:
<kml><NetworkLinkControl>
<minRefreshPeriod>0.0</minRefreshPeriod>
<maxSessionLength>-1.0</maxSessionLength>
<cookie>cookie=0|kmlRequestType=updates|projectID=6|lastUpdateSeenIndex=-1</cookie>
</NetworkLinkControl>
<Document id="myProject">
<name>My ProjectProject</name>
<Placemark id="pm1"><name>point1</name>
<Point><coordinates>-117.0,35.0</coordinates></Point>
</Placemark>
</Document></kml>
Later update KML served from http://server/kmlupdates/${CLIENT_ID}:
<kml><NetworkLinkControl>
<minRefreshPeriod>0.0</minRefreshPeriod>
<maxSessionLength>-1.0</maxSessionLength>
<cookie>cookie=0|kmlRequestType=updates|projectID=6|lastUpdateSeenIndex=0</cookie>
<Update>
<targetHref>http://server/kmlupdates/${CLIENT_ID}</targetHref>
<Change>
<Placemark targetId="pm1">
<name>Name changed by Update Change</name>
</Placemark>
</Change>
</Update>
</NetworkLinkControl></kml>
If anyone has any suggestions on what I'm missing here, I'd greatly appreciate it. Thanks!
My original version of the question left out two facts that turned out to be relevant: 1) I'm using the Java API for KML to generate this, and 2) the XML namespaces inside <kml>. I finally figured this out after reading this Google KML Group post for the umpteenth time.
The problem is the last XML namespace, "xmlns:xal". For some reason, removing that from the KML allows the <Update> tags to actually change the items in Google Earth. JAK doesn't let you change the namespaces, but you can strip it manually from the marshaled string.
Absolutely bizarre, but at least I found a solution.
As per https://developers.google.com/kml/documentation/kmlreference:
<xal:AddressDetails> is used by KML for geocoding in Google Maps only.
Currently, Google Earth does not use this element; use <address> instead.
I have some sample using different approach to do something else here maybe related to yours (as your purpose "send over an initial set of data, then update it dynamically using tags") :
https://sites.google.com/site/canadadennischen888/home/kml/auto-refresh-3d-tracking
Approach is that all changes are from server Restful service. Hope it helps. Details as :
How to make a dynamic Auto refresh 3D Tracking :
prepare a RestFul service to generate KML file from DB (KML sample as inside above link)
My other jsp code will generate a KMZ file which has a link to my Restful service. KMZ file has onInterval ( as in the bottom)
Jsp web page allow user to download KMZ file.
When Google Earth open KMZ file, Google Earth will auto refresh to get new data from that Restful service
Everytime refreshing, server will send the latest update KML data with new data to GE.
KMZ sample:
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2"
xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
<NetworkLink>
<name>Dennis_Chen_Canada#Hotmail.com</name>
<open>1</open>
<Link>
<href>http://localhost:9080/google-earth-project/rest/kml/10001/20002</href>
<refreshMode>onInterval</refreshMode>
</Link>
</NetworkLink>
</kml>

Resources