KML Example

Very simple example of displaying a kml file in Google Maps

by sachinsharma9780

HTML

<title>Google Maps API v3 : KML Layer</title>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false">
</script>

<body onload="display_kmlmap()">
    
    I have one function display_map. I have stripped the options to a minimum.<br/>
    Instead of adding a listener - I simply call the function in the body onload.<br/>
   
<div id="map_canvas" style="width:500px; height:400px; float:left">
</div>
</body>

JavaScript

function display_kmlmap()
{
    // A map needs 2 things - a place to put it and map options
    
    var map_options = { };  
    var map = new google.maps.Map(document.getElementById("map_canvas"),map_options);

    // OK - now we have a map, now let's display some kml - for this you need
    // to create a kmlLayer. You can add multple of these to a map in the kmlOptions
    
    // A kml layer needs 2 things - a kml file and a set of options
    // I selected a random kml file - but since I did not give a location for the 
    // map in map options - the kml file better do this 
    
    var kmlUrl = 'https://developers.google.com/maps/documentation/javascript/examples/kml/westcampus.kml';
   var kmlOptions = { map: map};

    // Create the kmlLayer - and you are done
    var kmlLayer = new google.maps.KmlLayer(kmlUrl, kmlOptions);
}