3
votes

I'm trying to build a map with a series of checkboxes in the sidebar, to display groups of markers. My KML file is structured as below (it's come from Google Earth):

    <document>
        <folder>
            <placemark>
                <xx />
                <xxx />
            </placemark>
        </folder>
     </document>

I'm able to load the KML and apply it to the map as a layer, showing all markers and polygons etc at once. What I'm not sure of, and can't find documented anywhere, is how to manipulate particular nodes in KML - say by way of a checkbox to display all placemarks in a particular folder, or to show all polygons, but no markers or vice versa.

Due to the number of placemarkers, I'd rather use Google Earth to maintain the KML file, not add them individually via javascript/API. Would I be better off using Fusion Tables?

To further complicate things, in some instances I'd like to display all placemarks from one folder and one from another - I'm thinking I'll use ExtendedData to achieve that, or explicitly add the marker by name. Get to that later...

2
Hi. You marked an answer as correct but further down mention Fusion tables. I'm desperately trying to find an answer to this stackoverflow.com/questions/16446118/…32423hjh32423

2 Answers

3
votes

As far as I know, you're correct - there's no way to access the Javascript objects Google Maps creates when you add a KmlLayer. I don't even think they exist - Google Maps v3 often renders layers as overlayed tiles that get rendered on the server side, so there might not be any Marker or Polygon objects in the way you expect (see the API docs).

This leaves you with two options:

  1. Create multiple KML files, one for each set of placemarks you'd like to show separately, and load them as separate KML layers. You can hide/show them by using myKmlLayer.setMap(map) and myKmlLayer.setMap(null). The Javascript here is relatively simple, but it's harder to manage your data and you're limited to the granularity of one full KML file - you can't show/hide individual placemarks.

  2. Load and parse your KML in your own code, using the resulting data to load markers and polygons onto the map. You can then use the references to these objects to support your show/hide controls. This could be a lot of work, though you might get some mileage with geoxml3.

You might also be able to catch references by listening to addoverlay, as described in this answer to a related question.

0
votes

There is a library I've used to do exactly what your describing (sidebar and everything): http://code.google.com/p/geoxml/. There's also http://code.google.com/p/geoxml-v3/ if you're using the v3 api.

Edit: Examples here: http://www.dyasdesigns.com/geoxml/

I started using separate kml files, but it gets a bit hairy, and the interface gets muddled with the globe icons for each file (instead of folder icons). I ended up using one kml file I manipulated to get the proper folder structure, and just keep adding to it with Google Earth.

The main issue is managing larger amounts of data (in the hundreds of points or shapes) gets awful. You can only move one at a time with Google Earth. Leave a comment if you find a better solution.