Google Fusion table 地图嵌入无法在 iPad 上运行

Google Fusion table map embed not working on iPad

我正在使用从 Google Fusion table 嵌入的地图,它在桌面上似乎工作正常,但没有出现在 iPad。

我不确定如何修复 iPad,所以我一直在使用 Chrome 扩展 'User Agent Switcher' 并在开发人员工具中使用设备模式。当我这样做时,我收到控制台错误:

"Uncaught TypeError: Cannot read property 'style' of null"

有人知道这是什么原因吗?

Codepen 在这里:http://codepen.io/anon/pen/KzVLvR

<meta name="viewport"></meta>

<notextile><style type="text/css">
#googft-mapCanvas {
  height: 500px;
  margin: 0;
  padding: 0;
  width: 100%;
}
</style></notextile>

<script type="text/javascript" src="https://maps.google.com/maps/api/js?sensor=false&amp;v=3"></script>

<script type="text/javascript">
  function initialize() {
    google.maps.visualRefresh = true;
    var isMobile = (navigator.userAgent.toLowerCase().indexOf('android') > -1) ||
      (navigator.userAgent.match(/(iPod|iPhone|iPad|BlackBerry|Windows Phone|iemobile)/));
    if (isMobile) {
      var metaTag=document.createElement('meta');
metaTag.name = "viewport"
metaTag.content = "width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"
document.getElementsByTagName('head')[0].appendChild(metaTag);
    }
    var mapDiv = document.getElementById('googft-mapCanvas');
    mapDiv.style.width = isMobile ? '100%' : '100%';
    mapDiv.style.height = isMobile ? '100%' : '500px';
    var map = new google.maps.Map(mapDiv, {
      center: new google.maps.LatLng(50.86921697720649, 0.18021480234369847),
      zoom: 10,
      mapTypeId: google.maps.MapTypeId.ROADMAP

    });
    map.controls[google.maps.ControlPosition.RIGHT_BOTTOM].push(document.getElementById('googft-legend-open'));
    map.controls[google.maps.ControlPosition.RIGHT_BOTTOM].push(document.getElementById('googft-legend'));

          var myLatLng = {lat: 50.878353, lng: 0.063805};

    var marker = new google.maps.Marker({
    position: myLatLng,
    map: map,
    title: 'Glyndebourne',
    icon: 'http://res.cloudinary.com/glyndebourne/image/upload/v1456245664/gicon_afipi7.png'
  });

    layer = new google.maps.FusionTablesLayer({
      map: map,
      heatmap: { enabled: false },
      query: {
        select: "col4",
        from: "1_wBUixHJqO_W95zMHk_eP8wQKBuXvHEfvNgfTBSC",
        where: ""
      },
      options: {
        styleId: 4,
        templateId: 4
      }


    });

    if (isMobile) {
      var legend = document.getElementById('googft-legend');
      var legendOpenButton = document.getElementById('googft-legend-open');
      var legendCloseButton = document.getElementById('googft-legend-close');

// CONSOLE ERROR REFERS TO THIS NEXT LINE:  
      legend.style.display = 'none'; 

      legendOpenButton.style.display = 'block';
      legendCloseButton.style.display = 'block';
      legendOpenButton.onclick = function() {
        legend.style.display = 'block';
        legendOpenButton.style.display = 'none';
      }
      legendCloseButton.onclick = function() {
        legend.style.display = 'none';
        legendOpenButton.style.display = 'block';
      }
    }
  }

  google.maps.event.addDomListener(window, 'load', initialize);
</script>

<div id="googft-mapCanvas"></div>

尽管与问题无关,但请完全删除此部分:

    if (isMobile) {
      var legend = document.getElementById('googft-legend');
      var legendOpenButton = document.getElementById('googft-legend-open');
      var legendCloseButton = document.getElementById('googft-legend-close');

// CONSOLE ERROR REFERS TO THIS NEXT LINE:  
      legend.style.display = 'none'; 

      legendOpenButton.style.display = 'block';
      legendCloseButton.style.display = 'block';
      legendOpenButton.onclick = function() {
        legend.style.display = 'block';
        legendOpenButton.style.display = 'none';
      }
      legendCloseButton.onclick = function() {
        legend.style.display = 'none';
        legendOpenButton.style.display = 'block';
      }
    }

问题是这样的:

mapDiv.style.width = isMobile ? '100%' : '100%';
mapDiv.style.height = isMobile ? '100%' : '500px';

在移动设备上,脚本会将 mapDiv 的宽度和高度设置为 100%。 设置百分比值需要为父节点(body)也设置这些值(width/height)....但它们未设置,因此可能无法计算mapDiv的宽度和高度。

修复:将 htmlbody 的 width/height 设置为 100%

/**CSS**/
html,body{
  height:100%;
  width:100%;
}