css - 使用 jquery 移动和谷歌地图时的样式问题——需要刷新以适合

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

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

当我第一次加载索引页面并单击 map 页面时,然后是 I see only one small part (左上角)被谷歌地图占据。但是如果我直接加载 map 页面,它会正确加载:

再说一次:如果我直接转到带有 map 的页面,效果会很好!

这是 fiddle :http://jsfiddle.net/ggrEH/7/

最佳答案

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

我修改了你的例子:

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

希望对您有所帮助。

关于css - 使用 jquery 移动和谷歌地图时的样式问题——需要刷新以适合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15693310/

相关文章:

html - 滚动到伪框架网站上的 anchor 隐藏标题

android - 如何根据来自api的位置在android中的谷歌地图上移动标记

jquery - 如何使用 jQuery mobile 创建覆盖框

javascript - 判断元素是否存在

css - Angular ng-table 的水平滚动

Jquery Animate 由于某种原因无法工作

html - 如何在透明div中制作非透明元素

python - 使用 Python 在手机中进行实时 gps 跟踪

google-maps - Google map API 警告 : NoApiKeys

jquery - 需要阻止 div 在 JQM 中滚动