Google Earth API Javascript Tour动画

时间:2015-03-18 05:12:42

标签: google-earth

我正试图在javascript中实施Google地球API之旅。我尝试了 parsekml 方法,但我无法进行任何巡视。我有一个问题...带有Google Earth API的javascript支持游览吗?我不想使用 fetchkml 或从任何其他链接导入。这里我附上了我的示例代码。任何帮助......?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
  <title>Google Earth-Practice</title>
  <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
  <style type="text/css">@import "static/examples.css";</style>
  <style type="text/css">@import "static/prettify/prettify.css";</style>
  <script type="text/javascript" src="static/prettify/prettify.js"></script>
  <script type="text/javascript" src="http://www.google.com/jsapi?hl=en&amp;key=ABQIAAAAwbkbZLyhsmTCWXbTcjbgbRSzHs7K5SvaUdm8ua-Xxy_-2dYwMxQMhnagaawTo7L1FE1-amhuQxIlXw"></script>
  <script type="text/javascript">
  /* <![CDATA[ */
  var ge;
var i=1;;
var l=1;
var Excel;
var value;
  google.load("earth", "1");

  function init() {

    google.earth.createInstance('map3d', initCB, failureCB);
  }

  function initCB(instance) {
    ge = instance;
    ge.getWindow().setVisibility(true);


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

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

    document.getElementById('installed-plugin-version').innerHTML =ge.getPluginVersion().toString();
  }

  function failureCB(errorCode) {
  }



function my_function()
{

return Excel.Workbooks.Open("C:/TEST.xls").ActiveSheet.Cells(l,i).Value;
}





//where l is number of rows and i are columns...
 function DrawVector()
 {

//where l is number of rows and i are columns...
//var i=1;
//var l=1;
var z=1;
var line=[];
document.getElementById('loadingImage').style.visibility='visible'; 
document.getElementById('Fly').style.visibility='visible'

Excel = new ActiveXObject("Excel.Application");
Excel.Visible = false;

do
{
    a=my_function()

    if (a!=null)
    {
    //var t=prompt("hello");
        line[z]=a;
    z++;
        i++;
    }
    else
    {
        l++;
        i=1;

        //document.write("<br />");
    }

    b = my_function()
}while(a!=null || b!=null);


    var lineStringPlacemark = ge.createPlacemark('');
    var lineString = ge.createLineString('');
    lineStringPlacemark.setGeometry(lineString);
    lineString.setExtrude(true);

    lineString.setAltitudeMode(ge.ALTITUDE_RELATIVE_TO_GROUND);

// Add LineString points


for(i=1;i<z;i=i+4)
{
var placemark = ge.createPlacemark('');

var icon = ge.createIcon('');
icon.setHref('https://cdn1.iconfinder.com/data/icons/Map-Markers-Icons-Demo-PNG/256/Map-Marker-Flag--Right-Chartreuse.png');
var style = ge.createStyle(''); //create a new style
style.getIconStyle().setIcon(icon);
style.getIconStyle().setScale(3.0); //apply the icon to the style
//style.getColor().set('ffffffff');
placemark.setStyleSelector(style); //apply the style to the placemark
// Set the placemark's location.  

var point = ge.createPoint('');
point.setLatitude(line[i+1]);
point.setLongitude(line[i+2]);
point.setAltitude(line[i+3]);
point.setAltitudeMode(ge.ALTITUDE_RELATIVE_TO_GROUND);
placemark.setGeometry(point);

placemark.setName(line[i]);

// Add the placemark to Earth.
ge.getFeatures().appendChild(placemark);
    lineString.getCoordinates().pushLatLngAlt(line[i+1],line[i+2],line[i+3]);

}// lineString.getCoordinates().pushLatLngAlt(38.885278,-77.701944,1900);
//  lineString.getCoordinates().pushLatLngAlt(38.769167,-77.901944,2700);


// Create a style and set width and color of line
    lineStringPlacemark.setStyleSelector(ge.createStyle(''));
    var lineStyle = lineStringPlacemark.getStyleSelector().getLineStyle();
    lineStyle.setWidth(4);
     var polyColor = lineStringPlacemark.getStyleSelector().getPolyStyle().getColor(); 
    //polyColor.set('ffffffff');
        polyColor.setA(127);
    polyColor.setB(0); 
    polyColor.setG(255);
    polyColor.setR(0);





    //lineStyle.polystyle.color = '990000ff';
    lineStyle.getColor().set('ffffffff');  // aabbggrr format
    //polystyle color = 66FFF700;
    //lineString.getPolyStyle(polystyle);// Add the feature to Earth
    ge.getFeatures().appendChild(lineStringPlacemark);





document.getElementById('loadingImage').style.visibility='hidden';




 }


  function showSun() {
    ge.getSun().setVisibility(true);
    ge.getTime().getControl().setVisibility(ge.VISIBILITY_SHOW);



  }

 function KmlView()
 {  DrawVector();
    var name='start';
    var kmlString = ''
              + '<?xml version="1.0" encoding="UTF-8"?>'
+ '<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2">'
+ '  <Document>'
+'<Camera> <altitudeMode>relativeToGround</altitudeMode>'
              + '    <longitude>-77.451111</longitude>'
              + '    <latitude>38.935278</latitude>'
              + '    <altitude>10000 </altitude>'
              + '    <heading>-105.948935</heading>'
              + '    <tilt>93</tilt>'
          + '<range>50</range>'
              + '  </Camera>'
+ '    <open>1</open>  '
+ '    <gx:Tour>     '
+ '      <name>Play me</name>     '
+ '      <gx:Playlist>   '
+ '        <gx:FlyTo>     '
+ '          <gx:duration>2.0</gx:duration>   '
+ '          <gx:flyToMode>bounce</gx:flyToMode>      '
+ '          <LookAt>    '
+ '            <longitude>-77.451111</longitude>  '   
+ '            <latitude>   38.935278</latitude> '        
+ '            <altitude>900</altitude>        '
+ '            <heading>-123.948935</heading>         '
+ '            <tilt>63.0957450</tilt>        '
+ '            <range>469.850414</range>        '
+ '            <gx:altitudeMode>relativeToGround </gx:altitudeMode>      '
+ '          </LookAt>   '
+ '        </gx:FlyTo>  '
+ '        <gx:AnimatedUpdate>    '
+ '          <gx:duration>0.0</gx:duration> '
+ '          <Update>  '
+ '            <targetHref/>    '
+ '            <Change>   '
+ '              <Placemark targetId="Fly1">    '
+ '                <gx:balloonVisibility>1</gx:balloonVisibility>      '
+ '              </Placemark>  '
+ '            </Change>    '
+ '          </Update>     '
+ '        </gx:AnimatedUpdate>    '
+ '        <gx:AnimatedUpdate>     '
+ '          <gx:duration>0.0</gx:duration>    '
+ '          <Update>       '
+ '            <targetHref/>    '
+ '            <Change>            '
+ '              <Placemark targetId="Fly1">'
+ '                <gx:balloonVisibility>0</gx:balloonVisibility>     '
+ '              </Placemark>        '
+ '            </Change>     '
+ '          </Update>       '
+ '        </gx:AnimatedUpdate>  '
+ '        <gx:FlyTo>       '
+ '          <gx:duration>20</gx:duration>       '
+ '          <gx:flyToMode>smooth</gx:flyToMode>  '
+ '          <LookAt>     '
+ '            <longitude>-77.701944</longitude>   '
+ '            <latitude>38.885278</latitude>  '
+ '            <altitude>1900</altitude>       '
+ '         <heading>-123.948935</heading>  '
+ '            <tilt>63.117317</tilt>        '
+ '            <range>792.665540</range>    '
+ '            <gx:altitudeMode>relativeToGround</gx:altitudeMode>       '
+ '          </LookAt>      '
+ '        </gx:FlyTo>         '
+ '        <gx:AnimatedUpdate> '
+ '          <gx:duration>0.0</gx:duration>  '
+ '          <Update>       '
+ '            <targetHref/>    '
+ '            <Change>         '
+ '              <Placemark targetId="Fly2">      '
+ '                <gx:balloonVisibility>1</gx:balloonVisibility>      '
+ '              </Placemark>          '
+ '            </Change>     '
+ '          </Update>        '
+ '        </gx:AnimatedUpdate> '
+ '          <gx:AnimatedUpdate>  '
+ '          <gx:duration>0.0</gx:duration>        '
+ '          <Update>     '
+ '            <targetHref/>       '
+ '            <Change>        '
+ '              <Placemark targetId="Fly2">     '
+ '                <gx:balloonVisibility>0</gx:balloonVisibility>  '
+ '              </Placemark>          '
+ '            </Change>       '
+ '          </Update>    '
+ '        </gx:AnimatedUpdate>    '
+ '        <gx:FlyTo>       '
+ '          <gx:duration>30</gx:duration>'
+ '          <gx:flyToMode>smooth</gx:flyToMode>     '
+ '          <LookAt>         '
+ '            <longitude>-77.901944</longitude>    '
+ '            <latitude>38.769167</latitude>     '
+ '            <altitude>2700</altitude>     '
+ '            <heading>-123.948935</heading> '
+ '            <tilt>63.063392</tilt>         '
+ '            <range>633.666023</range>     '
+ '            <altitudeMode>relativeToGround</altitudeMode>   '
+ '          </LookAt>   '
+ '        </gx:FlyTo>   '
+ '        <gx:AnimatedUpdate>      '
+ '          <gx:duration>0.0</gx:duration> '
+ '          <Update>     '
+ '            <targetHref/>  '
+ '            <Change>         '
+ '              <Placemark targetId="onland">       '
+ '                <gx:balloonVisibility>1</gx:balloonVisibility>  '
+ '              </Placemark>      '
+ '            </Change>       '
+ '          </Update>   '
+ '        </gx:AnimatedUpdate>    '
+ '   </gx:Playlist>     '
+ '    </gx:Tour>  '
+ '    <Placemark id="Fly1">   '
+ '      <name>   </name>    '
+ '      <description>    '
+ '           KIAD  Starting point...         </description>     '
+ '      <Point>      '
+ '        <gx:altitudeMode>relativeToGround</gx:altitudeMode>    '
+ '      </Point>   '
+ '    </Placemark>  '
+ '    <Placemark id="Fly2">     '
+ '      <name></name>  '
+ '      <description>We reached LOULU...</description> '
+ '      <Point>       '
+ '        <gx:altitudeMode>relativeToGround</gx:altitudeMode>   '
+ '      </Point> '
+ '    </Placemark> '
+ '     <Placemark id="onland">     '
+ '       <name></name>   '
+ '       <description>     '
+ '         <![CDATA[ RNDLI.   '
+ '       You<gx:balloonVisibility>1</gx:balloonVisibility>  '
+ '        Reached your destination]]>   '
+ '       </description>     '
+ '       <Point>      '
+ '       </Point>     '
+ '     </Placemark>  '
+ '        <ScreenOverlay>'
+ '     <name>Absolute Positioning: Top left</name>'
+ '     <visibility>1</visibility>'
+ '     <Icon>'
+ '       <href>https://cdn4.iconfinder.com/data/icons/32x32-free-design-icons/32/Target.png</href>'
+ '     </Icon>'
+ '     <overlayXY x="0.5" y=".5" xunits="fraction" yunits="fraction"/>'
+ '     <screenXY x="0.5" y=".5" xunits="fraction" yunits="fraction"/>'
+ '     <rotation>2.37878630116985</rotation>'
+ '     <size x=".1" y=".1" xunits="fraction" yunits="fraction"/>'
+ '   </ScreenOverlay>'
+ '       </Document>'
+ ' </kml>';
    var kmlObject = ge.parseKml(kmlString);
    ge.getFeatures().appendChild(kmlObject);
     ge.getView().setAbstractView(kmlObject.getAbstractView());
//ge.getTourPlayer().setTour(kmlObject);
  // ge.getTourPlayer().play();


   }



  function hideSun() {
    ge.getSun().setVisibility(false);

  }

  /* ]]> */
  </script>
</head>
<body onload="if(window.prettyPrint)prettyPrint();init();" bgcolor=#87CEEB>
  <h1>Google Earth -Practice</h1>
  <dl>

  </dl>
  <div style="clear: both;"></div>

  <div id="ui" style="position: relative;">
    <div id="map3d_container" style="border: 1px solid #000; width: 800px; height: 800px;">
      <div id="map3d" style="height: 100%;"></div>
    </div>

    <div id="extra-ui" style="position: absolute; left: 820px; top: 0;">
      Starting Point:<input type="text" name="fname">&nbsp;&nbsp;
  Destination Point: <input type="text" name="lname"><br><br/><br/>
  <center><input type="button"  onclick="KmlView()" value="Start"/><br/><br/></center><br/><br/>
    <form id="Fly" style="visibility:hidden">

 <input type="button" onclick="showSun()" value="Show Sun"/><br/><br/><br/>
      <input type="button" onclick="hideSun()" value="Hide Sun"/><br/><br/><br/>
      <img id="loadingImage" src="C:\Users\h118093\Downloads\Load2.gif" height="50%" width="50%" style="visibility:hidden" alt="Ready To Fly...."/>

</form>

    </div>
  </div>

</body>
</html>

如何在不使用 fetchkml 的情况下使用javascript在Google地球上进行游览?

1 个答案:

答案 0 :(得分:0)

根据Google的Earth API网站,通过javascript API支持Touring,只播放已在KML中编码的游览。 Google的Touring API网页:https://developers.google.com/earth/documentation/touring

上述网页链接中的Google文档包含如何在KML中调用游览的示例。您上面提供的代码只是将KML加载到Google地球插件中。您的代码不会调用游览。

下一步是通过该页面上显示的ge.getTourPlayer()。setTour(object)和ge.getTourPlayer()。play()方法正确调用巡视。你应该能够利用&#34;简单游览&#34;示例显示在Google的巡回文档网页上。

我试用了你的代码,并根据Google巡回网页上的信息正确编码后进行巡视。但是,你的kmlString也有一些错误。你没有关闭/ Document和/ kml标签。并获得简单的游览&#34;为了工作,你实际上需要完全删除Document标签。

[仅供参考...你知道这个API会在2015年12月12日被Google关闭吗?如果您打算在今年年底之前使用它,您将需要开始寻找旅行选择。]

编辑回答添加代码更改:

var kmlString = ''
+ '<?xml version="1.0" encoding="UTF-8"?>'
+ '<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2">'
//+ '  <Document>'  //<---------delete this line
:
: <clipped>
:
+ '    </gx:Tour>'
+ '</kml>'; //<---------add this line

kmlObject = ge.parseKml(kmlString);
ge.getFeatures().appendChild(kmlObject);
//ge.getView().setAbstractView(kmlObject.getAbstractView());    //<---------delete this line unless you need it for other purposes
ge.getTourPlayer().setTour(kmlObject);              //<---------add these two lines
ge.getTourPlayer().play();