【问题标题】:How to add Google Earth KML file to html iframe?如何将 Google 地球 KML 文件添加到 html iframe?
【发布时间】:2014-10-27 23:35:29
【问题描述】:

基本上,我正在尝试将我的 Google 地球 div 上的 KML 文件添加/显示到我的网站。我称我的文件为“satellites.kml”。

<!-- html code starts...-->

 <p><iframe 
       name="myKMLEarth" 
       src="/getrack/satellites.kml"
       height="440" 
       width="100%" 
       frameborder="0" 
       scrolling="no">
 </iframe></p>

<!-- html code continues ...-->

当页面加载时,它会下载我的 KML,而不是在 iframe 中打开它。我不应该使用 src 链接到 KML 文件吗?任何意见,将不胜感激!提前谢谢!

【问题讨论】:

  • 你想做什么?将 .kml 文件的联系人显示为纯文本?
  • 我正在尝试将我的 KML 图层添加到网站上显示的 Google 地球。
  • 你为什么使用 iframe?您可以将satellites.kml 添加为新层。请参阅Here 了解其他人是如何做到的
  • 我之前尝试过这样做,但它在本地主机上不起作用。当我上传到服务器时,添加src="http://someserver.com/my.kml" 那工作。我不知道,是不是你的问题。控制台说什么?
  • 对不起,我忘了提到,我已经在谷歌地图 api3 中做到了。如我所见,并非如此

标签: html iframe kml google-earth


【解决方案1】:

您必须使用 Google Maps JavaScript API https://developers.google.com/maps/documentation/javascript/reference?hl=es

您必须将google.maps.KmlLayer 附加到地图。

将API脚本放入&lt;head&gt;

<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&amp;sensor=false"></script>

创建一个像这样的 div:

<div id="google-map" class="google-map"></div>

然后,在&lt;/body&gt; 之前使用这个JS 代码。设置您的纬度、经度和 KML 文件的路径。

<script>
    function initialize() {
        var mapOptions = {
          center: new google.maps.LatLng(YOUR_LAT,YOUR_LNG), //Set your latitude, longitude
          zoom: 19,
          mapTypeId: google.maps.MapTypeId.SATELLITE,
          scrollwheel: false
        }

        var map = new google.maps.Map(document.getElementById('google-map'), mapOptions); // get the div by id

        var ctaLayer = new google.maps.KmlLayer({
          url: 'PATH/TO/FILE.kml' // Set the KML file
        });

        // attach the layer to the map
        ctaLayer.setMap(map);
    }

    // load the map
    google.maps.event.addDomListener(window, 'load', initialize);
</script>

【讨论】:

    【解决方案2】:

    您似乎需要在另一个应用程序中或使用插件打开 KML。浏览器不知道如何在 iframe 中显示文件,所以它只是下载它。

    From Google's KML Documentation: “许多应用程序都显示 KML,包括 Google Earth、Google Maps、Google Maps for mobile、NASA WorldWind、ESRI ArcGIS Explorer、Adobe PhotoShop、AutoCAD 和 Yahoo! Pipes。”

    我不能 100% 理解您想要做什么,所以我不能确定这是否是您正在寻找的,但这个问题可能会为您指明正确的方向:How to Embed KML files (Google Earth) into a website without the google gadget?

    【讨论】:

      【解决方案3】:

      如果您可以使用 Google 地图而不是 Google 地球,则可以使用此处给出的 KMLLayer:https://developers.google.com/maps/tutorials/kml/

      仅供参考:Google 已弃用 Google Earth API - https://developers.google.com/earth/documentation/index

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-30
        • 1970-01-01
        相关资源
        最近更新 更多