只为一种地图类型打开 Google Fusion Table/为某些地图类型移除 Google Fusion table

Only open Google Fusion Table for one map type/ Remove Google Fusion table for certain map types

我正在尝试使用 Google 地图 Javascript API 制作网络地图。我希望波士顿建筑物的 Google 融合 Table 仅出现在我称为 "Buildings." 的程式化地图上 当我单击“建筑物”地图类型时,建筑物图层出现。然后,当我点击道路和卫星地图时,建筑物层就会消失,就像我想要的那样。但是,如果我先单击道路或卫星地图类型,然后单击建筑物地图类型,则不会加载建筑物图层。我不知道为什么。谢谢!

<!DOCTYPE html>
<html>
<head>
<h3 align="center">Boston, MA</h3>
<title>Massachusetts, USA</title>
<meta name="Running Destinations" content="initial-scale=1.0, user-scalable=no">
<meta name="author" content="CodingGrandma">
<meta charset="utf-8">
<style>
    /* formatting for html containing the map */
    body {background-color: White;}
    h3 {color: Black;}
    /* formatting for map */
  #map {
    height: 75%;
  }
  html, body {
    height: 100%;
    margin: 1;
    padding: 0;
  }
    </style>
  </head>
  <body>
    1 foot<input type="radio" name="Storm Surge" value="1 foot" id="1_ft_surge"/><br/>
    2 foot<input type="radio" name="Storm Surge" value="2 foot" id="2_ft_surge"/><br/>
<div id="map"></div>
<script>
var map;
var mapType;
var layer = new google.maps.FusionTablesLayer(null);
function initMap() {
    // Create Styled Map Object 
    var styledMapType = new google.maps.StyledMapType(
        [
{
    "featureType": "administrative",
    "stylers": [
        {
            "visibility": "off"
        }
    ]
},
{
    "featureType": "poi",
    "stylers": [
        {
            "visibility": "simplified"
        }
    ]
},
{
    "featureType": "road",
    "elementType": "labels",
    "stylers": [
        {
            "visibility": "simplified"
        }
    ]
},
{
    "featureType": "water",
    "stylers": [
        {
            "visibility": "simplified"
        }
    ]
},
{
    "featureType": "transit",
    "stylers": [
        {
            "visibility": "simplified"
        }
    ]
},
{
    "featureType": "landscape",
    "stylers": [
        {
            "visibility": "simplified"
        }
    ]
},
{
    "featureType": "road.highway",
    "stylers": [
        {
            "visibility": "off"
        }
    ]
},
{
    "featureType": "road.local",
    "stylers": [
        {
            "visibility": "on"
        }
    ]
},
{
    "featureType": "road.highway",
    "elementType": "geometry",
    "stylers": [
        {
            "visibility": "on"
        }
    ]
},
{
    "featureType": "water",
    "stylers": [
        {
            "color": "#84afa3"
        },
        {
            "lightness": 52
        }
    ]
},
{
    "stylers": [
        {
            "saturation": -17
        },
        {
            "gamma": 0.36
        }
    ]
},
{
    "featureType": "transit.line",
    "elementType": "geometry",
    "stylers": [
        {
            "color": "#3f518c"
        }
    ]
}
],
        //Remember to include name for map style you created
        {name: 'Buildings'});
      map = new google.maps.Map(document.getElementById('map'), {
      center: {lat: 42.340885, lng: -71.047289},
      zoom: 13,
      panControl:true,
      scaleControl:true,
      zoomControl:true,
      zoomControlOptions:{
        style: google.maps.ZoomControlStyle.LARGE},
      mapTypeControl: true,
      streetViewControl:true,
      overviewMapControl:true,
      rotateControl:true,
      zoomControlOptions:{
        style:google.maps.ZoomControlStyle.LARGE,
        position:google.maps.ControlPosition.RIGHT_CENTER
        },
      //add your style to MapTypeOptions below
      mapTypeControlOptions: {
        mapTypeIds: ['roadmap', 'satellite', 'hybrid', 'terrain',
                'styled_map']
      }
    });
    google.maps.event.addListener(map, 'maptypeid_changed', function() {
        mapType = map.getMapTypeId();
        AddBuildings()
    });
    map.mapTypes.set('styled_map',styledMapType);
  }
     function AddBuildings(){
if (mapType == "styled_map") {
         layer = new google.maps.FusionTablesLayer({
            query:{
            select: 'geometry',
            from: '1mzPT_gVGQRujbzIdPTzV8swo2AI611PEwIKwzgj7',
            }
        });
        layer.setMap(map);
    } else {
        layer.setMap(null);
  }
  }

</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBV-z3Aar9YGZXept2AXxLY_70TL317mPA&callback=initMap">
</script>

发布的代码出现 javascript 错误:

  • Uncaught ReferenceError: google is not defined
  • Uncaught TypeError: Cannot read property 'setMap' of undefined.

在加载 Google 地图 Javascript API v3 之前,您无法使用它。

移动layer的初始化:

var layer = new google.maps.FusionTablesLayer(null);

initMap 函数(如 mapmapType)中,该函数在 API 完成加载并可供使用后运行。

var layer;
function initMap() {
  layer = new google.maps.FusionTablesLayer(null);

proof of concept fiddle (loads the API synchronously)

代码片段:

var map;
var mapType;
var layer;

function initMap() {
  layer = new google.maps.FusionTablesLayer(null);
  // Create Styled Map Object 
  var styledMapType = new google.maps.StyledMapType(
    [{
        "featureType": "administrative",
        "stylers": [{
          "visibility": "off"
        }]
      },
      {
        "featureType": "poi",
        "stylers": [{
          "visibility": "simplified"
        }]
      },
      {
        "featureType": "road",
        "elementType": "labels",
        "stylers": [{
          "visibility": "simplified"
        }]
      },
      {
        "featureType": "water",
        "stylers": [{
          "visibility": "simplified"
        }]
      },
      {
        "featureType": "transit",
        "stylers": [{
          "visibility": "simplified"
        }]
      },
      {
        "featureType": "landscape",
        "stylers": [{
          "visibility": "simplified"
        }]
      },
      {
        "featureType": "road.highway",
        "stylers": [{
          "visibility": "off"
        }]
      },
      {
        "featureType": "road.local",
        "stylers": [{
          "visibility": "on"
        }]
      },
      {
        "featureType": "road.highway",
        "elementType": "geometry",
        "stylers": [{
          "visibility": "on"
        }]
      },
      {
        "featureType": "water",
        "stylers": [{
            "color": "#84afa3"
          },
          {
            "lightness": 52
          }
        ]
      },
      {
        "stylers": [{
            "saturation": -17
          },
          {
            "gamma": 0.36
          }
        ]
      },
      {
        "featureType": "transit.line",
        "elementType": "geometry",
        "stylers": [{
          "color": "#3f518c"
        }]
      }
    ],
    //Remember to include name for map style you created
    {
      name: 'Buildings'
    });
  map = new google.maps.Map(document.getElementById('map'), {
    center: {
      lat: 42.340885,
      lng: -71.047289
    },
    zoom: 13,
    panControl: true,
    scaleControl: true,
    zoomControl: true,
    zoomControlOptions: {
      style: google.maps.ZoomControlStyle.LARGE
    },
    mapTypeControl: true,
    streetViewControl: true,
    overviewMapControl: true,
    rotateControl: true,
    zoomControlOptions: {
      style: google.maps.ZoomControlStyle.LARGE,
      position: google.maps.ControlPosition.RIGHT_CENTER
    },
    //add your style to MapTypeOptions below
    mapTypeControlOptions: {
      mapTypeIds: ['roadmap', 'satellite', 'hybrid', 'terrain',
        'styled_map'
      ]
    }
  });
  google.maps.event.addListener(map, 'maptypeid_changed', function() {
    mapType = map.getMapTypeId();
    AddBuildings()
  });
  map.mapTypes.set('styled_map', styledMapType);
}

function AddBuildings() {
  if (mapType == "styled_map") {
    layer = new google.maps.FusionTablesLayer({
      query: {
        select: 'geometry',
        from: '1mzPT_gVGQRujbzIdPTzV8swo2AI611PEwIKwzgj7',
      }
    });
    layer.setMap(map);
  } else {
    layer.setMap(null);
  }
}
/* formatting for html containing the map */
body {
  background-color: White;
}
h3 {
  color: Black;
}
/* formatting for map */
#map {
  height: 75%;
}
html,
body {
  height: 100%;
  margin: 1;
  padding: 0;
}
1 foot<input type="radio" name="Storm Surge" value="1 foot" id="1_ft_surge" /><br/> 2 foot<input type="radio" name="Storm Surge" value="2 foot" id="2_ft_surge" /><br/>
<div id="map"></div>
<script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBV-z3Aar9YGZXept2AXxLY_70TL317mPA&callback=initMap"></script>