【问题标题】:Google Fusion table map embed not working on iPadGoogle Fusion 表格地图嵌入在 iPad 上不起作用
【发布时间】:2016-03-08 11:18:33
【问题描述】:

我正在使用从 Google Fusion 表中嵌入的地图,它在桌面上似乎可以正常工作,但在 iPad 上却不显示。

我不确定如何在 iPad 上修复,所以我一直在使用 Chrome 扩展程序“用户代理切换器”并在开发者工具中使用设备模式。当我这样做时,我得到控制台错误:

“未捕获的类型错误:无法读取 null 的属性 'style'”

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

在此处编写代码: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>

【问题讨论】:

    标签: google-maps google-maps-api-3 google-fusion-tables


    【解决方案1】:

    虽然与问题无关,但将这部分彻底删除:

        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的宽度/高度设置为100%

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

    【讨论】:

    • 感谢莫勒博士。 CSS 修复有效,但为什么我需要删除第一部分?我刚试了,加载不出来。
    • 也许你删除了太多......你应该删除的部分会在移动设备上强制出错(因为文档中没有#googft-legend)。修复代码笔:codepen.io/anon/pen/WwrqzB
    猜你喜欢
    • 1970-01-01
    • 2013-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-30
    • 1970-01-01
    • 1970-01-01
    • 2018-04-16
    相关资源
    最近更新 更多