【问题标题】:Google Earth Callbacks Not FiringGoogle 地球回调未触发
【发布时间】:2021-12-02 16:22:47
【问题描述】:

我正在尝试让 Google 地球回调正常工作,但它们似乎并没有为我触发。我在 Google 地球 API 站点上使用了 View Change Example 作为参考,并且发现它与我的代码之间没有任何区别……但我的代码不起作用!

以下是我的代码的重要部分:

<script type="text/javascript">
var ge;

google.load("earth", "1");

function init() {
  document.getElementById('map3d').innerHTML = '';
  google.earth.createInstance('map3d', initCallback, failureCallback);
}

function initCallback(pluginInstance) {
  ge = pluginInstance;
  ge.getWindow().setVisibility(true);

  // add a navigation control
  ge.getNavigationControl().setVisibility(ge.VISIBILITY_AUTO);

  // Set the FlyTo speed.
  ge.getOptions().setFlyToSpeed(ge.SPEED_TELEPORT);

  // add some layers
  //ge.getLayerRoot().enableLayerById(ge.LAYER_BORDERS, true);
  //ge.getLayerRoot().enableLayerById(ge.LAYER_ROADS, true);

  throw (new Error("InitCallback!"));

  google.earth.addEventListener(ge.getView(), 'viewchange', function () {
      throw (new Error("EVENT!"));
  });

}

function failureCallback(errorCode) {
}
</script>

“初始化回调!”错误被正确抛出,但我从未看到“事件!”抛出错误——无论我如何移动地球。

有人看到我做错了吗?非常感谢任何帮助!

【问题讨论】:

    标签: javascript dom-events google-earth google-earth-plugin


    【解决方案1】:

    我不是 javascript 方面的专家,但如果你替换:

           throw (new Error("EVENT!"));
    

    使用警报代替,例如:

           alert('EVENT!');
    

    我打赌你会发现它会起作用。即 eventListener 正在工作,它似乎不喜欢 'throw' 命令

    【讨论】:

    • 不幸的是,事件确实没有触发。执行此替换不会导致弹出警报。原始代码 - throw(new Error(...)) - 是出于调试目的将某些内容打印到错误控制台的快速脏方法。但最终我在事件回调中的任何内容都没有被调用。
    • 这很有趣,因为如果您转到您提到的视图更改示例页面,然后单击“在谷歌代码游乐场玩此示例”按钮 - 在该页面上,当我放一个“ throw' 到 initCallback() 中它会起作用,但是 eventListener 中的'throw' 将不起作用。然而,警报将在这两个地方起作用 - 尽管事件监听器中的一个会导致我的浏览器冻结:)
    • 很奇怪。我不知道为什么它以前不起作用......我可能有其他东西坐在周围导致问题。事件确实在触发并删除了 throw(Error()) 修复了它。
    【解决方案2】:

    lifeIsGood 是对的——当你像上面那样抛出一个没有 try catch 块的错误时,会导致抛出错误后的 js 不执行。

    如果您只是尝试使用“快速肮脏的方式”在控制台中打印某些内容,请改用 console.log('Error');...

    function initCallback(pluginInstance) {
         ge = pluginInstance;
         ge.getWindow().setVisibility(true);
    
         //throw (new Error("InitCallback!")); -- change this to 
         console.log('InitCallback!');
    
         google.earth.addEventListener(ge.getView(), 'viewchange', function () {
             //throw (new Error("EVENT!")); -- change this to
             console.log('EVENT!');
         });
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-12
      • 2020-08-08
      • 1970-01-01
      • 2015-01-30
      • 1970-01-01
      相关资源
      最近更新 更多