KML在Local GE上工作,不在HTML中

时间:2013-01-22 07:14:49

标签: kml

我是Google地球的新手。我正在尝试开发一个应用程序来可视化地图上的天气数据。 这是简单的代码,可以在本地GE运行(我只是从其他地方复制样本)

<?xml version="1.0" encoding="utf-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
  <Document>
    <Style id="s1">
      <LineStyle>
        <color>7f0000ff</color>
        <width>4</width>
      </LineStyle>
      <PolyStyle>
        <color>7f0000ff</color>
        <colorMode>normal</colorMode>
        <fill>1</fill>
        <outline>1</outline>
      </PolyStyle>
    </Style>

    <name>All isolation countries</name>
    <description>All isolation countries</description>
    <Placemark>
      <styleUrl>#s1</styleUrl>
      <name>Indonesia</name>
    </Placemark>
    <Placemark>
      <styleUrl>#s1</styleUrl>
      <name>Ecuador</name>
      <Polygon>
      <tessellate>1</tessellate>
        <extrude>1</extrude>
        <altitudeMode>clampedToGround</altitudeMode>
        <outerBoundaryIs>
          <LinearRing>
            <coordinates>-90.61167907714844,-0.3755556046962738,0 -90.77166748046875,-0.344166785478592,0 -90.87222290039063,-0.2652778923511506,0 -90.79332733154297,-0.149444505572319,0 -90.77362060546876,-0.1550000011920929,0 -90.58556365966797,-0.2455555945634842,0 -90.55029296875,-0.3091666996479035,0 -90.61167907714844,-0.3755556046962738,0</coordinates>
          </LinearRing>
        </outerBoundaryIs>

      </Polygon>
    </Placemark>
    </Document>
    </kml>

以下是我在HTML中集成的代码

<html>
<head>
  <title>Sample</title>
  <script type="text/javascript" src="https://www.google.com/jsapi"> </script>
  <script type="text/javascript">
    var ge;
    var placemark;
    var object;

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

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

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


     var kmlString = ''
     + '<?xml version="1.0" encoding="UTF-8"?>'
     + '<kml xmlns="http://www.opengis.net/kml/2.2">'
     + '<Document>'
     + ' <Style id="s1">'
     + ' <LineStyle>'
     + '  <color>7f0000ff</color>'
       + ' <width>4</width>'
      +'</LineStyle>'
     +' <PolyStyle>'
      +'  <color>7f0000ff</color>'
       +' <colorMode>normal</colorMode>'
       +' <fill>1</fill>'
       +' <outline>1</outline>'
     +' </PolyStyle>'
    +'</Style>'
    +'<name>All isolation countries</name>'
    +'<description>All isolation countries</description>'
    +'<Placemark>'
     +' <styleUrl>#s1</styleUrl>'
     +' <name>Indonesia</name>'
    +'</Placemark>'
    +'<Placemark>'
     +' <styleUrl>#s1</styleUrl>'
      +'<name>Ecuador</name>'
      +'<Polygon>'
      +'<tessellate>1</tessellate>'
        +'<extrude>1</extrude>'
        +'<altitudeMode>clampedToGround</altitudeMode>'
        +'<outerBoundaryIs>'
          +'<LinearRing>'
            +'<coordinates>-90.61167907714844,-0.3755556046962738,0 -90.77166748046875,-0.344166785478592,0 -90.87222290039063,-0.2652778923511506,0 -90.79332733154297,-0.149444505572319,0 -90.77362060546876,-0.1550000011920929,0 -90.58556365966797,-0.2455555945634842,0 -90.55029296875,-0.3091666996479035,0 -90.61167907714844,-0.3755556046962738,0</coordinates>'
          +'</LinearRing>'
        +'</outerBoundaryIs>'
     +' </Polygon>'
    +'</Placemark>'
                         + '</Document>'
                         + '</kml>';

         var kmlObject = ge.parseKml(kmlString);
         ge.getFeatures().appendChild(kmlObject);
         ge.getView().setAbstractView(kmlObject.getAbstractView());
    }

    function failureCB(errorCode) {
    }

    google.setOnLoadCallback(init);
  </script>

</head>
<body>
  <div id="map3d" style="height: 400px; width: 600px;"></div>
</body>
</html>

有人能给我一些想法吗?此外,我需要大量的纬度,经度和高度数据输入,有没有办法读取这些文件?

2 个答案:

答案 0 :(得分:1)

JavaScript Maps API将从公共可访问的URL接受KML - 您无法传入字符串/文件。如果您需要使用本地KML文件,则必须自己解析KML并使用Google Maps API创建形状。在此处查看此优秀文章:Google Maps API and KML File LocalHost Development Options

答案 1 :(得分:0)

正如Matthew所说,Google地图需要使用远程URI来获取KML:将其保存为带有公共URI的文件。

GeoXML3可以阅读并映射KML,并且不受此限制。