0

我正在使用这个示例代码,我对以下部分的作用以及它们的来源感到困惑。我不明白(\'red\')onclick 的部分或+ file +两者之一。我正在尝试将其更改为createNetworkLink并使用我自己的 tester.kml。当我更改+ file +它时,它会中断。

    var currentKmlObjects = {
      'red': null,
    };

addSampleUIHtml(
        '<h2>Toggle KML Files:</h2>' +
        '<input type="checkbox" id="kml-red-check" onclick="toggleKml(\'red\');"/><label for="kml-red-check">Red Placemarks</label><br/>'
      );

     function loadKml(file) {
      var kmlUrl = 'http://earth-api-samples.googlecode.com/svn/trunk/' +
        'examples/static/' + file + '.kml';

      // fetch the KML
      google.earth.fetchKml(ge, kmlUrl, function(kmlObject) {
        // NOTE: we still have access to the 'file' variable (via JS closures)

        if (kmlObject) {
          // show it on Earth
          currentKmlObjects[file] = kmlObject;
          ge.getFeatures().appendChild(kmlObject);
        } else {
          // bad KML
          currentKmlObjects[file] = null;
4

2 回答 2

0

Look at the definition of the loadkml function.

function loadKml(file) { ...

the word file is simply a parameter, whatever you pass to the function will be represented by file within the scope of the function.

If ones calls loadKml("FOO") then the kmlUrl within the function would be equal to

http://earth-api-samples.googlecode.com/svn/trunk/examples/static/FOO.kml

Now, you don't show your toggleKml function- but it too looks to accept a single parameter just like loadkml does. It will look something like.

function toggleKml(file) { ...

So, when you click toggleKml("red"); is called, passing the value "red" into it. So, within the scope of the method the word file will have the value red, if you called toggleKml("orange"); - the word file will have the value orange - and so on.

于 2013-12-15T23:08:52.640 回答
0

“红色”只是一个例子。如果您要加载的 kml 是“mydoc.kml”,则将红色替换为 mydoc。将http://earth-api-samples.googlecode.com/svn/trunk/ ' + 'examples/static/ 替换为您的文件在网络上的位置。不要触摸+file+。你必须有这样的东西:' http : //myadress.com/file/'+文件+'.kml'。

于 2013-11-25T14:45:50.040 回答