【问题标题】:Only open Google Fusion Table for one map type/ Remove Google Fusion table for certain map types仅针对一种地图类型打开 Google Fusion Table/删除某些地图类型的 Google Fusion Table
【发布时间】:2018-10-02 05:16:03
【问题描述】:

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

<!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 html google-maps google-maps-api-3 google-fusion-tables


    【解决方案1】:

    发布的代码出现 javascript 错误:

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

    在加载之前,您无法使用 Google Maps 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)

    代码 sn-p:

    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>

    【讨论】:

      猜你喜欢
      • 2013-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多