3

We have a KMZ file that loads into the Google Earth Desktop application just fine. No errors.

When we try to use the Google Earth Plugin to do the same thing, it does not even return from the fetchKml function. Any special settings we need to know about to use fetchKml on a local file?

I am trying to load the file like so:

// Where 'ge' is the Google Earth Plugin
var href = 'C:/KMLDATA/TEST.KMZ';
google.earth.fetchKml(ge, href, function(kml) { /* do something with kml */ });
Fraser
  • 15,275
  • 8
  • 53
  • 104
Paul Smith
  • 51
  • 2
  • 3

1 Answers1

3

This has nothing to do with the Google Earth Plugin as such, rather it is to do with the JavaScript sandbox.

Basically JavaScript has no access to the local file system - so you can't simply use a path to a local file such as you have in your code...

var href = 'C:/KMLDATA/TEST.KMZ';
google.earth.fetchKml(ge, href, function(kmlObject) { ... }

To work with local files in the browser you have a number of options.

  1. Set up a local file server and server the file over http. This is relatively easy to do in any OS. So that C:/KMLDATA/TEST.KMZ might become http://localhost/KMLDATA/TEST.KMZ

  2. Use some 'plugin' object that can access the file system. A bit more tricky and hard to get working across all browsers. Some thing like ActiveX, XPCOM, Signed Java applets, etc. I made an example of loading local .kml files into the plug-in via ActiveX - obviously it will only work in IE.

  3. Use the file api in HTML5. A lot of code and not something I have actually tried with kml. This tutorial is pretty thorough and covers most aspects well.

I would say that by far option 1 is your best bet. Setting up a local file server will allow you to load and test all your kml/kmz file easily.

If none of that is possible or desirable for you then hosting the files on a public server, as others have suggested, is really the only option.

Fraser
  • 15,275
  • 8
  • 53
  • 104
  • Have you had this working? It was my understanding that this would not work in some situations unless the URL was also accessible to Google as well. If I am incorrect you have also made my day, as option 1 is easy enough to do and not have to expose certain content directly to the public. – Matthew Feb 01 '13 at 21:28
  • From here: https://developers.google.com/kml/documentation/mapsSupport : When KML content containing a large number of features (placemarks, polygons, lines) is loaded into Google Maps, a server-side decision is made about whether or not to render the document's features on the server or in the user's web browser. The user experience, including feature appearance and interactivity, will not be affected by this decision; the content will look and behave the same way, regardless of the chosen method. I don't think any option is viable that doesn't involve parsing the KML yourself. – Matthew Feb 01 '13 at 23:29
  • @Matthew yes I have it working.I even wrote a tiny server specifically for it (although any server will do)...the server is part of this control library - http://code.google.com/p/winforms-geplugin-control-library/ – Fraser Feb 02 '13 at 00:40
  • @Matthew Also, that excerpt is specifically about Google Maps and how it renders large kml documents. It has nothing at all to do with the Google Earth Plugin - or the sandbox in JavaScript that prevents local file access. Loading local kml/kmz files via fetchKml works perfectly well if you serve those files over http. I do it all the time...Further more, if one can access the kml as a string, no matter how that string is obtained, then it is simply a case of calling GEPlugin.parseKml(kmlString) on it to have the plug-in convert the string into the relevant KmlObjects. – Fraser Feb 02 '13 at 00:41
  • Thanks for the info! I have spent less time with the GE Plugin, but thought that I had this confirmed as the same issue. I need to go correct another answer now as well I think. – Matthew Feb 02 '13 at 06:51