Jonathan Smith
Jonathan Smith

Reputation: 2599

Jquery mobile dialog not displaying page

I have taken the basic maps demo from the Google Maps JavaScript API V3 site and placed it in a page called "testnav.htm". I am trying to open this page as a dialog using JQM, but the dialog does not display correctly. All i get is the dialog header. My testnav.htm looks like this:

<!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>

And i am calling it like this:

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

How come the map canvas does not display?

UPDATE Following Omar's advice, this is the code i am running with now....

<!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>

But i am not sure where to add the google.maps.event.addDomListener(window, 'load', initialize);

Upvotes: 0

Views: 760

Answers (1)

Apostolos Emmanouilidis
Apostolos Emmanouilidis

Reputation: 7197

I have created two examples where the map opens inside a jQM dialog. The first example contains a standalone dialog with a map. The second example contains jQM page with a link. The dialog opens after clicking the link.

<!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>

I hope this helps.

Upvotes: 1

Related Questions