Search code examples
google-mapsgoogle-fusion-tablesremoveall

gmaps remove FusionTablesLayer from map


I'm having a bit of an issue removing the fusion tables layer from my map after it has been added. I would like to modify my addRoute function (below) so that it removes any existing fusion tables layers before adding the new layer to the map. Thanks in advance for the help!

function addRoute() {            
        var route = document.getElementById("route").value;
        var layer = new google.maps.FusionTablesLayer({
            query:
            {
                name: 'fusionLayer',
                select: 'name',
                from: '1I6lehOOrJJ-BkPznjdiIDnr0o9J8EqAnu73f6Ic',
                where: "name = '" + route + "'"
            }
        });            
        map.loadFromFusionTables(layer);
    }

Solution

  • Looks like you might be using gmap3.js ("loadFromFusionTables")

    If you keep a reference to the layer outside of the "addRoute" function, you can set its "map" property to null to remove it from the map:

    layer.setMap(null);
    

    in the Google Maps API v3. Or you could change the route without destroying and recreating the layer:

    var route = document.getElementById("route").value;
    layer.setOptions({query:
            {
                name: 'fusionLayer',
                select: 'name',
                from: '1I6lehOOrJJ-BkPznjdiIDnr0o9J8EqAnu73f6Ic',
                where: "name = '" + route + "'"
            }})