Why Is My KmlLayer in Google Maps V3 Not Working?

If you use some code like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<script type="text/javascript">
  function initialize() {
    var options = {
      mapTypeId: google.maps.MapTypeId.SATELLITE,
      streetViewControl: true
    };
    var map = new google.maps.Map(document.getElementById('map'), options);
    var kmlLayer = new google.maps.KmlLayer('http://127.0.0.1:3000/kmlfile.kml');
    kmlLayer.setMap(map);
  }

  function loadScript() {
    var script = document.createElement("script");
    script.type = "text/javascript";
    script.src = "http://maps.google.com/maps/api/js?sensor=false&callback=initialize";
    document.body.appendChild(script);
  }
  window.onload = loadScript;

</script>

In a typical Ruby on Rails development environment you will have your server running on your localhost on port 3000. The way Google Maps accesses your KML file is not directly from the script locally, but via Google itself. So the solution is to have your KML online accessible by Google Maps. I hope this tip saved you some valuable time! ;-)

Comments

Copyright © 2013 - Tom Pesman - Powered by Octopress