如何在google earth插件中传递文本框的内容来设置摄像头



目前我正在与谷歌地球插件代码工作。此时我正在尝试根据某些文本框的内容来移动摄像头。这是我目前拥有的代码。

var ge;
google.load("earth", "1");
function init() {
  google.earth.createInstance('map3d', initCallback, failureCallback);
//create boxes
  addSampleUIHtml(
    '<input id="Altitude" type="text" value="500000"/>');
  addSampleUIHtml(
    '<input id="Longitude" type="text" value="2"/>');
  addSampleUIHtml(
    '<input id="Latitude" type="text" value="42"/>');  
  addSampleUIHtml(
    '<input id="Heading" type="text" value="0"/>');  
  addSampleUIHtml(
    '<input id="Tilt" type="text" value="0"/>');
  addSampleUIHtml(
    '<input id="Roll" type="text" value="0"/>');    
  addSampleButton('Move the camera', buttonClick);
}
function initCallback(instance) {
  ge = instance;
  ge.getWindow().setVisibility(true);
  // just for debugging purposes
  document.getElementById('installed-plugin-version').innerHTML =
    ge.getPluginVersion().toString();
}
function failureCallback(errorCode) {
}
function buttonClick() {
  var AltitudeInBox=document.getElementById('Altitude').value
  var CameraAt= ge.getView().copyAsCamera(ge.ALTITUDE_ABSOLUTE);
  CameraAt.set   (  41.383, // latitude +-90
                               2.183 , // longitude +-180
                               AltitudeInBox, // altitude (m)
                               ge.ALTITUDE_ABSOLUTE, // reference altitude mode
                               0,  //heading Direction (that is, North, South, East, West), in degrees. Default=0 (North). Values range from 0 to 360 degrees
                               0, //Tilt 0 to 360 degrees.
                               0 //Rotation, in degrees around the Z axis. -180 to +180 degrees.
  ge.getView().setAbstractView(CameraAt)
}

当我将变量AltitudeInBox定义为box Altitude的内容时,value被传递给this。我使用警报函数(alert(AltitudeInBox);)进行检查,但是当我将变量AltitudeInBox作为函数CameraAt的参数传递时。但我没有得到任何结果。我怀疑变量AltitudeInBox被分配为图表而不是数字,但不知道如何更改它。提前谢谢你。

好了,我找到解决办法了。这是初学者犯的错误。下面的语句:

 var AltitudeInBox=document.getElementById('Altitude').value

将海拔框的内容指定为图表变量,但下面的代码需要一个数字

   CameraAt.set   (  41.383, // latitude +-90
                               2.183 , // longitude +-180
                               AltitudeInBox, // altitude (m)
                               ge.ALTITUDE_ABSOLUTE, // reference altitude mode
                               0,  //heading Direction (that is, North, South, East,West), in degrees. Default=0 (North). Values range from 0 to 360 degrees
                               0, //Tilt 0 to 360 degrees.
                               0 //Rotation, in degrees around the Z axis. -180 to +180degrees.
解决方案是使用parseFloat()或parseInt(),如下所示。
   CameraAt.set   (  parseFloat(document.getElementById('Latitude').value),
                     parseFloat(document.getElementById('Longitude').value) ,
                     parseFloat(document.getElementById('Altitude').value), 
                     ge.ALTITUDE_ABSOLUTE, //altitude mode
                     parseFloat(document.getElementById('Heading').value),
                     parseFloat(document.getElementById('Tilt').value),
                     parseFloat(document.getElementById('Roll').value)    )

最新更新