简体   繁体   中英

Google map didn't load in jquery mobile

I don't know why when i try to display map on my app it's not working..just grey, but when i ran it on Jsfiddle, everything is fine. I think it's about the pageshow, if i change it to pageinit, the map is not fully loaded and i have change all the height and width to 100 % but still not fully loaded..

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

i do try add this code but, the map is fully loaded but the marker is not centered..

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

and the second thing is the title marker not showing up. please help.. thank you

Reset the center position of the map after triggering the resize event.

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

(after making currentPositionMarker global like the map variable is)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM