问题
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 */ });
回答1:
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.
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 becomehttp://localhost/KMLDATA/TEST.KMZ
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.
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.
来源:https://stackoverflow.com/questions/14613726/unable-to-load-a-local-kmz-file-into-a-browser-with-the-google-earth-plugin-usin