Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add Google Earth KML file to html iframe?

Basically, I am trying to add/show my KML file on my Google Earth div to my website. I call my file "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 ...-->

When the page loads, it downloads my KML instead of opening it up in the iframe. Should I not use src to link to the KML file? Any advice would be appreciated! Thank you in advance!

like image 396
MadsterMaddness Avatar asked Oct 17 '25 16:10

MadsterMaddness


2 Answers

You have to use the Google Maps JavaScript API https://developers.google.com/maps/documentation/javascript/reference?hl=es

You have to attach a google.maps.KmlLayer to a Map.

Put the API script in the <head>

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

Create a div like:

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

Then, use this JS code before </body>. Set your latitude, longitude and path to KML file.

<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>
like image 158
alekstrust Avatar answered Oct 19 '25 07:10

alekstrust


It seems like you need to open the KML in another application or using a plugin. The browser does not know how to display the file in an iframe, so it simply downloads it.

From Google's KML Documentation: "Many applications display KML, including Google Earth, Google Maps, Google Maps for mobile, NASA WorldWind, ESRI ArcGIS Explorer, Adobe PhotoShop, AutoCAD, and Yahoo! Pipes."

I don't 100% understand what you are trying to do, so I can't be sure if this is what you are looking for, but this question might point you in the right direction: How to Embed KML files (Google Earth) into a website without the google gadget?

like image 43
Sally Maier Avatar answered Oct 19 '25 07:10

Sally Maier



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!