【问题标题】:JQuery - Placing a Google Map in a "content" isn't visible?JQuery - 在“内容”中放置谷歌地图不可见?
【发布时间】:2013-06-13 21:49:06
【问题描述】:

在使用 Google Maps v3 和 JQuery 时,在 data-role="content" 中放置地图有点麻烦。控制台中没有显示错误,所以我很难调试。屏幕上没有显示地图。

这是我的两个文件:

index.html

<!DOCTYPE html>
<html>
    <head>
    <meta charset="utf-8">
        <meta name="viewport" content="initial-scale=1, width=device-width, user-scalable=no" />
        <link rel="stylesheet" href="https://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.css" />
        <script type="text/javascript" src="https://code.jquery.com/jquery-1.9.1.min.js"></script>
        <script type="text/javascript" src="https://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.js"></script>
        <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?&sensor=true"></script>
        <script type="text/javascript" src="js/mapload.js"></script>
</head>
<body>
    <div data-role="page" id="map">
        <div data-role="header">
            <h1>Header</h1>
        </div>
        <div data-role="content">
            <div id="map-canvas"></div>
        </div>
    </div>
</body>
</html>

ma​​pload.js

function initialize()
var mapOptions = {
    center: new google.maps.LatLng(49,-8),
    zoom: 15,
    disableDefaultUI: true,
    mapTypeId: google.maps.MapTypeId.ROADMAP
};

var map = new google.maps.Map(document.getElementById("map-canvas"),mapOptions);

}

google.maps.event.addDomListener(window, 'load', initialize);

This 给了我一些见解,但没有完整回答我的问题。


编辑:解决方案

<!DOCTYPE html>
<html>
<head>
    <title>jQM Complex Demo</title>
    <meta name="viewport" content="initial-scale=1, maximum-scale=1"/>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
    <script type="text/javascript" src="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.js"></script>
    <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?key={REPLACE THE BRACKETS AND THIS TEXT WITH YOUR API KEY}&sensor=true"></script>    
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.css" />
</head>
<body>
<script type="text/javascript">
    $(document).on('pageinit', '#index',function(e,data){    
        var map = new google.maps.Map(document.getElementById('map_canvas'), {
            center: new google.maps.LatLng(11.618044,-34.823347),
            zoom: 15,
            disableDefaultUI: true,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        });
    });
    </script>
    <div data-role="page" id="index">
        <div data-theme="a" data-role="header">
            <h3>
                First Page
            </h3>
        </div>

        <div data-role="content" id="content" style="padding:0;position:absolute;top:40px;right:0;bottom:40px;left:0;">
            <div id="map_canvas" style="height:100%"></div>
        </div>

        <div data-theme="a" data-role="footer" data-position="fixed">
            <h3>
                First Page
            </h3>
        </div>
    </div>
</body>
</html>    

【问题讨论】:

    标签: javascript jquery css jquery-mobile google-maps-api-3


    【解决方案1】:

    jQuery Mobile 只有一种使用 Google 地图的方法。因为 jQuery Mobile 是特定的窗口加载不能用于初始化地图。

    需要特别正确的高度才能成功初始化地图,并且只能在 jQuery Mobile pageshow 页面事件期间完成。如果您想了解更多关于 jQuery Mobile 页面事件以及为什么 pageshow 事件对于插件初始化很重要,请查看我的私人博客 ARTICLE。或者你可以找到它HERE

    让我把这个故事简单地说一下,这是一个工作示例:http://jsfiddle.net/Gajotres/pkZHg/

    这是使用的 javascript 和 CSS:

    Javascript

    $(document).on('pageinit', '#index',function(e,data){    
        var map = new google.maps.Map(document.getElementById('map_canvas'), {
            center: new google.maps.LatLng(42.618044,-87.823347),
            zoom: 15,
            disableDefaultUI: true,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        });
    });
    

    CSS

    #content {
        padding: 0;
        position : absolute !important; 
        top : 40px !important;  
        right : 0; 
        bottom : 40px !important;  
        left : 0 !important;     
    }
    

    【讨论】:

      【解决方案2】:

      在 html 末尾加载 mapload.js

      <div data-role="page" id="map">
              <div data-role="header">
                  <h1>Header</h1>
              </div>
              <div data-role="content">
                  <div id="map-canvas"></div>
              </div>
          </div>
      <script type="text/javascript" src="js/mapload.js"></script>
      

      当您调用地图函数时,DOM 尚未准备好

      另一个错误可能是 CSS 样式。你可以试试这个

      设定的宽度和高度 `

      #map-canvas {
        width: 480px;
        height: 640px;
      }
      

      `

      【讨论】:

      • 这是一个错误。另一个是确保您的地图画布 css 是正确的.. 检查正文并查看地图画布内是否有任何实际内容,您可以找出如何在您的网页中使用最佳 CSS。那应该做的工作
      猜你喜欢
      • 1970-01-01
      • 2010-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-23
      • 1970-01-01
      相关资源
      最近更新 更多