【问题标题】:Styling issues when using jquery mobile and google maps -- needs refresh to fit使用 jquery mobile 和 google maps 时的样式问题——需要刷新以适应
【发布时间】:2013-03-19 13:38:09
【问题描述】:

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

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

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

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

【问题讨论】:

  • 如何加载地图页面,将其与事件绑定?如果将其绑定到 pageinit,则将其绑定到 pagebeforeshow,例如document).on('pagebeforeshow', '[data-role="page"]#maps' , function(){}
  • jsFiddle,链接到网站??
  • 用这个$(document).on('pagebeforeshow', '[data-role="page"]#maps', function() { //map code here });包装上面的代码。从正文中删除 onload
  • 我加了小提琴:jsfiddle.net/ggrEH/7

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


【解决方案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>

我希望这会有所帮助。

【讨论】:

  • 我不明白这个问题。你能解释一下吗?你的意思是哪个不必要的步骤?谢谢
  • 请忽略;我明白你的建议。然而,在我初始化地图后,它仍然有同样的问题。
  • 诀窍是在 $(document).on("pageshow", "#maps", function() {// 这里初始化 });您是否尝试在 pageshow 上执行初始化?
  • 我尝试在 pageshow jsfiddle.net/ggrEH/13 上实现;它以某种方式不起作用
  • 我已经更新了答案。我已经修改了您在 jsfiddle 上提供的代码,以便它可以工作。为了测试它,将它放在一个文件 ex: map.html 中并打开它。您的 jsfiddle 上的配置是错误的,因为您在加载时执行 JavaScript。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-02
  • 1970-01-01
相关资源
最近更新 更多