0

I need to build a simple map with checkboxes. Every checkbox must show different KML layer. I don't know how to build this, but in samples on Google Code I found below code.

But when I test all files on localhost (WAMP) KML layer is not showing when I click a checkbox. Other markers in JavaScript are showing normal, only KML is not.

Code to show KML layer is below. Please tell me how I can change code to grab markers.kml from specific URL and not in folder. I need to grab KML from other websites/servers.

/**
 * Toggles KML layer visibility.
 */
function toggleKmlLayer() {
  if (!kmlLayer) {
    var kmlUrl = window.location.href.substring(
        0, 1 + window.location.href.lastIndexOf('/')) + 'markers.kml';
    kmlLayer = new google.maps.KmlLayer(kmlUrl, {
      preserveViewport: false,
      suppressInfoWindows: false
    });
  }
  showKmlLayer = !showKmlLayer;
  if (showKmlLayer) {
    kmlLayer.setMap(map);
  } else {
    kmlLayer.setMap(null);
  }
}
Peter O.
  • 32,158
  • 14
  • 82
  • 96
Karnak
  • 21
  • 2
  • 7
  • 21

1 Answers1

2

Your KML needs to be at a publicly available URL. localhost is not publicly available (Google's servers can't access the KML).

from the documentation

KML and GeoRSS Layers

The Google Maps API supports the KML and GeoRSS data formats for displaying geographic information. These data formats are displayed on a map using a KmlLayer object, whose constructor takes the URL of a publicly accessible KML or GeoRSS file.

geocodezip
  • 158,664
  • 13
  • 220
  • 245
  • Thanks for this, but how can I change my code to grab KML file from live server (mydomain.com/markers.kml), but all javascript and html files is on localhost? Code in my question grab markers.kml from root folder, if I understand code? Again, I'm not a programmer, I just learn code with examples, but I must build this type of map for my project. Thanks for help! – Karnak Jun 02 '13 at 00:49
  • I don't understand your question. If the KML has a public URL, just use it. – geocodezip Jun 02 '13 at 04:56
  • Hey! Sorry for my bad English :( If I understand this line of code (0, 1 + window.location.href.lastIndexOf('/')) + 'markers.kml';) grab KML file from directory where is the JavaScript file. How can I change this line of code for grabing KML file from diferent URL and not the same directory where is the JS files? I hope you understand what I looking for. – Karnak Jun 02 '13 at 12:11
  • What is the public URL of the KML? Just use that (don't compute it from the location of the current page, which is what that code is doing). See the [example in the documentation](https://developers.google.com/maps/documentation/javascript/layers#KMLLayers) – geocodezip Jun 02 '13 at 14:12