0

我从 Google Maps JavaScript API V3 站点获取了基本地图演示,并将其放置在一个名为“testnav.htm”的页面中。我正在尝试使用 JQM 将此页面作为对话框打开,但该对话框无法正确显示。我得到的只是对话框标题。我的 testnav.htm 看起来像这样:

<!DOCTYPE html>
<html>
  <head>
    <title>Simple Map</title>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <style>
      html, body, #map-canvas {
        margin: 0;
        padding: 0;
        height: 200px;
        width: 400px;
      }
    </style>
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
    <script>
        var map;
        function initialize() {
            var mapOptions = {
                zoom: 8,
                center: new google.maps.LatLng(-34.397, 150.644),
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };
            map = new google.maps.Map(document.getElementById('map-canvas'),
                mapOptions);
        }
        console.log('here');
        google.maps.event.addDomListener(window, 'load', initialize);

    </script>
  </head>
  <body>
    <div data-role="dialog">
        <div data-role="header" data-theme="d">
            <h1>Map</h1>
        </div>
        <div data-role="content" data-theme="c">
            <div id="map-canvas"></div>          
        </div>

    </div>
  </body>
</html>

我这样称呼它:

<a href="testnav.htm" data-rel="dialog">View On Map</a>

为什么地图画布不显示?

更新遵循奥马尔的建议,这是我现在正在运行的代码......

<!DOCTYPE html>
<html>
  <head>
    <title>Simple Map</title>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">

  </head>
  <body>
      <div data-role="dialog">
          <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
            <script>
                $(document).on('pageshow', '[data-role=dialog]', function () {;
                    var map;
                    var mapOptions = {
                        zoom: 8,
                        center: new google.maps.LatLng(-34.397, 150.644),
                        mapTypeId: google.maps.MapTypeId.ROADMAP
                    };
                    map = new google.maps.Map(document.getElementById('map-canvas'),
                        mapOptions);
                    console.log('end');
                });

            </script>
        <div data-role="header" data-theme="d">
            <h1>Map</h1>
        </div>
        <div data-role="content" data-theme="c">       
            <div id="map-canvas" ></div>          
        </div>   
    </div>
  </body>

但我不确定在哪里添加google.maps.event.addDomListener(window, 'load', initialize);

4

1 回答 1

1

我创建了两个示例,其中地图在 jQM 对话框中打开。第一个示例包含一个带有地图的独立对话框。第二个示例包含带有链接的 jQM 页面。单击链接后将打开对话框。

<!doctype html>
<html lang="en">
   <head>
        <title>jQuery mobile with Google maps - Google maps jQuery plugin</title>
        <link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.css" />
        <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
        <script src="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.js"></script>
        <script type="text/javascript" src="http://maps.google.com/maps/api/js?v=3&sensor=false&language=en"> </script>
        <script type="text/javascript">

            var demoCenter = new google.maps.LatLng(41,-87),
                map;

            function initialize()
            {
                map = new google.maps.Map(document.getElementById('map_canvas'), {
                   zoom: 7,
                   center: demoCenter,
                   mapTypeId: google.maps.MapTypeId.ROADMAP
                 });
            }
            $(document).on("pageinit", "#basic-map", function() {
                initialize();
            });

        </script>
    </head>
    <body>
        <div id="basic-map" data-role="dialog">
            <div data-role="header">
                <h1><a href="/">jQuery mobile with Google maps v3</a> examples</h1>
                <a data-rel="back">Back</a>
            </div>
            <div data-role="content">   
                <div class="ui-bar-c ui-corner-all ui-shadow" style="padding:1em;">
                    <div id="map_canvas" style="height:350px;"></div>
                </div>
            </div>
        </div>      
    </body>
</html>

<!doctype html>
<html lang="en">
   <head>
        <title>jQuery mobile with Google maps - Google maps jQuery plugin</title>
        <link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.css" />
        <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
        <script src="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.js"></script>
        <script type="text/javascript" src="http://maps.google.com/maps/api/js?v=3&sensor=false&language=en"> </script>
        <script type="text/javascript">

            var demoCenter = new google.maps.LatLng(41,-87),
                map;

            function initialize()
            {
                map = new google.maps.Map(document.getElementById('map_canvas'), {
                   zoom: 7,
                   center: demoCenter,
                   mapTypeId: google.maps.MapTypeId.ROADMAP
                 });
            }
            $(document).on("pageinit", "#basic-map", function() {
                initialize();
            });

        </script>
    </head>
    <body>
        <div id="intro-page" data-role="page">
            <div data-role="header">
                <h1><a href="/">jQuery mobile with Google maps v3</a> examples</h1>
            </div>
            <div data-role="content">   
                <a data-rel="dialog" href="#basic-map">Open Map Dialog</a>
            </div>
        </div>
        <div id="basic-map" data-role="page">
            <div data-role="header">
                <h1><a href="/">jQuery mobile with Google maps v3</a> examples</h1>
                <a data-rel="back">Back</a>
            </div>
            <div data-role="content">   
                <div class="ui-bar-c ui-corner-all ui-shadow" style="padding:1em;">
                    <div id="map_canvas" style="height:350px;"></div>
                </div>
            </div>
        </div>      
    </body>
</html>

我希望这有帮助。

于 2013-06-10T18:43:39.233 回答