【问题标题】:How to parse KML file to Google Earth API on my website?如何在我的网站上将 KML 文件解析为 Google Earth API?
【发布时间】:2015-07-14 23:10:18
【问题描述】:

我创建了一个 KML 文件,并希望在我的网站上的 Google 地球 API 上展示它。我浏览了Google code playground

但我不知道应该使用哪一个来将 KML 解析为 Google 地球 API。这是我创建的 KML 文件的链接:http://g.co/maps/6zp5a

有人可以帮忙吗?

【问题讨论】:

    标签: kml google-earth google-earth-plugin


    【解决方案1】:

    您应该改用 fetchKml。

    http://code.google.com/apis/ajax/playground/?exp=earth#fetch_good_kml

    您还需要提供有效的网址,因此您需要使用完整的网址,而不是您的“短”网址,并在末尾添加“&output=kml”

    http://maps.google.com/maps/ms?msid=201182140457027399871.0004bee0c7348bce68977&msa=0&ll=9.438,39.5105&spn=130.55749,316.054688&output=kml

    将该网址放入示例代码页并运行它,它可以工作

    【讨论】:

    • 感谢您的建议!我试过了,但它没有显示任何点。运行 api 时是否看到任何彩色圆点?我还看到还有一个获取 KML(交互式)。我尝试将上面的 url 输入到框中,但它不起作用。
    • 我将我给你的 URL(末尾带有 &output=kml)放入了普通提取和交互式提取示例中。两者都为我加载地标。您需要手动移动视图才能看到它们,但通过更多编码,您可以加载页面并设置您自己的自定义视图。
    【解决方案2】:

    下面是“Hello Earth”演示,其中包含适用于您的 kml 的 fetch 功能。您很可能必须为您的 kml 中的图标设置样式以使用蓝色桨。

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html>
      <meta http-equiv="content-type" content="text/html; charset=utf-8" />
      <head>
    <!--
    Copyright 2008 Google Inc.
    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at
    
         http://www.apache.org/licenses/LICENSE-2.0
    
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
    -->
        <title>Hello Google Earth!</title>
        <script src="http://www.google.com/jsapi"></script>
        <script>
    google.load("earth", "1");
    
    var ge = null;
    
    function init() {
      google.earth.createInstance("map3d", initCallback, failureCallback);
    }
    
    function initCallback(object) {
      ge = object;
      ge.getWindow().setVisibility(true);
      function finished(object) {
        if (!object) {
          // wrap alerts in API callbacks and event handlers
          // in a setTimeout to prevent deadlock in some browsers
          setTimeout(function() {
            alert('Bad or null KML.');
          }, 0);
          return;
        }
        ge.getFeatures().appendChild(object);
      }
      // fetch the KML
      var url = 'http://maps.google.com/maps/ms?ie=UTF8&authuser=0&msa=0&output=kml&msid=201182140457027399871.0004bee0c7348bce68977';
      google.earth.fetchKml(ge, url, finished);
    
      document.getElementById('installed-plugin-version').innerHTML =
        ge.getPluginVersion().toString();
    }
    
    function failureCallback(errorCode) {
    }
      </script>
      </head>
      <body onload='init()' id='body'>
        <center>
          <div>
            Hello, Earth!
          </div>
          <div id='map3d' align="left" style='border: 1px solid silver; height: 600px; width: 800px;'></div>
        </center>
      </body>
    </html>
    

    【讨论】:

    • 非常感谢!精确点显示,但圆圈不显示。
    • 这是一个重新设计的带有蓝色桨叶的 kml。你应该可以用这个替换 url (maps.google.com/maps/…)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多