【问题标题】:Google map didn't load in jquery mobile谷歌地图未在 jquery mobile 中加载
【发布时间】:2015-03-17 17:58:06
【问题描述】:

我不知道为什么当我尝试在我的应用程序上显示地图时它不起作用..只是灰色,但是当我在 Jsfiddle 上运行它时,一切都很好。我认为这是关于 pageshow,如果我将其更改为 pageinit,则地图未完全加载,我已将所有高度和宽度更改为 100%,但仍未完全加载..

<html lang="en">
  <head>
    <title>Maps</title>
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css" />
    <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
    <script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.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 map;

        function initialize()
        {
            map = new google.maps.Map(document.getElementById('map_canvas'), {
               zoom: 7,
               center: new google.maps.LatLng(38.0457918, 23.7676337),
               mapTypeId: google.maps.MapTypeId.ROADMAP
             });
            var currentPositionMarker = new google.maps.Marker({
                    position: new google.maps.LatLng(38.0457918, 23.7676337),
                    map: map,
                    title :"hei"
                });
        }


        $(document).on("pageshow", "#basic-map", function() {
            initialize();
        });


    </script>
</head>
<body>
    <div id="basic-map" data-role="page">
        <div data-role="header">
            <h1><a data-ajax="false" href="/">examples</h1>
        </div>
        <div data-role="content"> 
            <p>this is paragraph</p>
              <ul data-role="listview" data-inset="true" data-divider-theme="b">
                  <li data-role="list-divider">Map</li>
                  <div id="map_canvas" style="height:320px;"></div> 
                </ul>

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

我确实尝试添加此代码,但地图已完全加载但标记未居中..

$( document ).bind( "pageshow", function( event, data ){
google.maps.event.trigger(map, 'resize');
});

第二件事是标题标记没有出现。请帮忙..谢谢你

【问题讨论】:

  • 您在什么设备上测试?它可以在我的 Mac 的 chrome、PC 的 IE、Android 的 Chrome 上运行...一件事是你没有关闭 &lt;a data-ajax="false" href="/"&gt; 除此之外,我觉得一切都很正常

标签: javascript google-maps jquery-mobile


【解决方案1】:

触发resize事件后重置地图的中心位置。

$( document ).bind( "pageshow", function( event, data ){
  google.maps.event.trigger(map, 'resize');
  map.setCenter(currentPositionMarker.getPosition());
});

(在使currentPositionMarkermap 变量一样成为全局变量之后)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-15
    • 2011-06-02
    • 1970-01-01
    • 1970-01-01
    • 2015-04-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多