0
votes

I have a KML layer of markers on a google map representing specific countries. When the user clicks on the marker I want it to take them to a specific URL for each marker. I've seen answers on here that explain how to add an onclick event when creating a marker, but I need to add separate onclick events to each marker from a KML file and pass a URL value from the KML file for the onclick event.

I have the URL value stored in the KML file like this: http://example.com/countries/usa/

I figured out that I can add an event listener to the KML layer that will respond to specific markers, but when I pass the marker object and log it in the console it doesn't have any of the information that was originally in the KML file.

google.maps.event.addListener(klmLayer, 'click', function(countryObject) {
  var marker = countryObject;
  console.log(marker);
});

Is there any way to pass a value from the KML file to the markerObject so I can use it in the above onclick function to direct the user to a specific URL? If not, what are my options?

Thanks!

2

2 Answers

0
votes

There's a way with geoXML3. I first wrote about it here. The idea is to pass a custom function as the parser reads the KML file. I wrote a simple example that reads this KML file, with URLs stored in the description and styleURL tags. The big disadvantage was that I couldn't figure out how to get geoXML3 to read other tags. Mouse over the markers to get one set of URLs and click to get the other set.

Another roadblock I faced was realizing I needed to place both html and KML in the same server because of Ajax. It did nothing when working offline.