0

我开始写我的移动网站,我遇到了以下问题:

当我第一次加载索引页面时,点击地图页面,然后我只看到一小部分(左上角)被谷歌地图占据。但是如果我加载地图页面directly,它会正确加载:

再说一遍:如果我直接进入带有地图的页面,效果很好!

这是小提琴:http: //jsfiddle.net/ggrEH/7/

4

1 回答 1

1

在 jQuery Mobile 中,默认情况下页面是通过 AJAX 加载的。这就是直接打开页面和通过导航打开页面的区别。建议使用 jQuery Mobile 事件来初始化地图。

我修改了你的例子:

<!DOCTYPE html> 
<html> 
    <head> 
        <title>Map Example Multiple Pages</title> 
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>jQuery mobile with Google maps</title>
        <meta content="en" http-equiv="content-language">
        <link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.0/jquery.mobile-1.3.0.min.css" />
        <style>
            #map_canvas{
                padding-top:0px;
                padding-bottom:-1px;
                padding-left:0px;
                height:auto;
                position:absolute;
                width:100%;
                height:95%;
                max-height:1600px;
                max-width:1600px;
                overflow:hidden;
                display:block;
            }
        </style>
        <script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
        <script src="http://code.jquery.com/mobile/1.3.0/jquery.mobile-1.3.0.min.js"></script>
        <script type="text/javascript" src="http://maps.google.com/maps/api/js?v=3&sensor=false&language=en"></script>
        <script>
            function initialize() {
                var mapCenter = new google.maps.LatLng(59.3426606750, 18.0736160278),
                myOptions = {
                    zoom:10,
                    mapTypeId: google.maps.MapTypeId.ROADMAP,
                    center: mapCenter
                },
                map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
            }

            $(document).on("pageshow", "#map-page", function() {
                if ($("#map_canvas").html() === '') {
                    initialize();
                }
            });
        </script>
    </head>

    <body>
        <div data-role="page" id="home-page">
            <!-- /header -->
            <div data-role="header">
                <h1>Maps</h1>
            </div>
            <!-- /content -->
            <div data-role="content">
                <a href="#map-page" data-role="button" data-transition="fade">Click to see the Map</a>
            </div>
        </div>

        <!-- /page -->
        <div data-role="page" id="map-page">
            <!-- /header -->
            <div data-role="header">
                <h1>Map</h1>
                <a href="#home-page" data-icon="home">Home</a>
            </div>
            <!-- /content -->
            <div data-role="content" style="width:100%; height:100%; padding:0; max-height:1600px;">
                <div id="map_canvas"></div>
            </div>
        </div>
    </body>
</html>

更新:

基于提供的 jsfiddle 的示例

<!DOCTYPE html> 
<html> 
    <head> 
        <title>Map Example Multiple Pages</title> 
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>jQuery mobile with Google maps</title>
        <meta content="en" http-equiv="content-language">
        <link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.0/jquery.mobile-1.3.0.min.css" />
        <style>
            .map_canvas{
                padding-top:0px;
                padding-bottom:-1px;
                padding-left:0px;
                height:auto;
                position:absolute;
                width:100%;
                height:95%;
                max-height:1600px;
                max-width:1600px;
                overflow:hidden;
                display:block;
            }
        </style>
        <script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
        <script src="http://code.jquery.com/mobile/1.3.0/jquery.mobile-1.3.0.min.js"></script>
        <script type="text/javascript" src="http://maps.google.com/maps/api/js?v=3&sensor=false&language=en"></script>
        <script>
            var map, geocoder, initCenter;
            function initialize() {
                geocoder = new google.maps.Geocoder();
                initCenter = new google.maps.LatLng(37, -97);
                var mapCenter = new google.maps.LatLng(59.3426606750, 18.0736160278),
                myOptions = {
                    zoom:10,
                    mapTypeId: google.maps.MapTypeId.ROADMAP,
                    center: mapCenter
                },
                map = new google.maps.Map(document.getElementById("googlemaps1"), myOptions);
            }

            $(document).on("pageshow", "#maps", function() {
                if ($("#googlemaps1").html() === '') {
                    initialize();
                }
            });
        </script>
    </head>
    <body>
        <div id="home" data-role="page" data-title="Home">
            <div data-role="header" data-position="fixed" data-id="vs_header">
                 <h1>Home</h1>
                <a href="#maps" data-icon="info">maps</a> 
            </div>
            <div data-role="content" id="index-page">
                <div>
                    <p>dsfgsjdg dskfjgskdfsdbf</p>
                </div>
            </div>
        </div>

        <!-- Page: maps -->
        <div id="maps" data-role="page" data-title="Fleet">
            <div data-role="header" data-position="fixed" data-id="vs_header" data-dom-cache="false">
                 <h1>Fleet</h1>
                <a href="#home" data-icon="info" data-iconpos="notext">Home</a>
            </div>
            <!-- header -->
            <div data-role="content" id="map" style="width:100%; height:100%; padding:0; max-height:1600px;">
                <div id="googlemaps1" class="map_canvas"></div>
            </div>
        </div>
    </body>
</html>

我希望这有帮助。

于 2013-03-31T18:09:47.307 回答