Shahjalal
Shahjalal

Reputation: 1151

How can I display a full screen google map with jQuery Mobile?

The following code displays strange output. I should see a full screen mobile map. But for some reason it shows on just a portion of the screen. I am using jquery.ui.map for mapping.

<!DOCTYPE html> 
<html> 
    <head> 
    <title>My Page</title> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <link rel="stylesheet" href="css/jquery.mobile-1.1.0.min.css" />
    <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script>
    <script src="js/jquery-1.7.1.min.js"></script>
    <script src="js/jquery.mobile-1.1.0.min.js"></script>
    <script type="text/javascript" src="js/jquery.ui.map.min.js"></script>
</head> 
<body> 

<div data-role="page">

    <div data-role="header">
        <h1>My Title</h1>
    </div><!-- /header -->

    <div data-role="content" id="map_canvas"  name="contentMain">   

    </div><!-- /content -->

</div><!-- /page -->
<script>

    $('#map_canvas').gmap().bind('init', function() { 
    // This URL won't work on your localhost, so you need to change it
    // see http://en.wikipedia.org/wiki/Same_origin_policy
    $.getJSON( 'http://jquery-ui-map.googlecode.com/svn/trunk/demos/json/demo.json', function(data) { 
        $.each( data.markers, function(i, marker) {
            $('#map_canvas').gmap('addMarker', { 
                'position': new google.maps.LatLng(marker.latitude, marker.longitude), 
                'bounds': true 
            }).click(function() {
                $('#map_canvas').gmap('openInfoWindow', { 'content': marker.content }, this);
            });
        });
    });
});
</script>
</body>
</html>

Output:

enter image description here

Thanks in advance.

Upvotes: 7

Views: 16493

Answers (4)

Liran H
Liran H

Reputation: 10569

I've been messing around with this for a bit, and managed to find the perfect solution. It caters for pages with or without a header. It will position the map perfectly in the space between the header and the bottom of the page (or on the whole page if a header is not present), for any resolution and any header height.

This solution requires both CSS and JS if the page has a header, and CSS only if the page does not have a header.

HTML

<div data-role="page" id="mapPage">
    <div data-role="header"`>
        <!-- your header HTML... -->
    </div>

    <div data-role="content">
        <div id="map-canvas"></div>
        <p id="nogeolocation"></p>
    </div>
</div>

CSS

#map-canvas {
    position: absolute;
    width: 100%; 
    height: 100%;
    top: 0;
    left: 0;
}

/* eliminates scrollbars in map infowindow (optional) */
#mappopup {
    line-height: 1.35;
    overflow: hidden;
    white-space: nowrap;
}

JS

var mapPageHeader = $("#mapPage .ui-header").eq(0);
if(mapPageHeader.length > 0) {
    var headerHeight = mapPageHeader.outerHeight();
    $("#map-canvas").css({
        'height': ($(window).height() - headerHeight + 1) + 'px',
        'top': headerHeight - 1
    });
}

Upvotes: 0

Energetic Codfish
Energetic Codfish

Reputation: 11

Header and footer are unavoidable if you're using jQuery. I set {data-position="fixed" data-fullscreen="true"} for both header and footer, and set html { height: 100% } body { height: 100%; margin: 0; } #map { position:absolute; width: 100%; height: 100%; padding: 0; } as well, so that both the menu and the map perfectly work together on the mobile device.

Upvotes: 1

Simon Kelly
Simon Kelly

Reputation: 554

The container and map canvas need to be 100% width and height.

From the jQuery mobile website their structure is:

<div data-role="page" id="map-page" data-url="map-page">
    <div data-role="header" data-theme="c">
        <h1>Maps</h1>
    </div>
    <div data-role="content" id="map-canvas">
        <!-- map loads here... -->
    </div>
</div>

And the CSS:

#map-page, #map-canvas { width: 100%; height: 100%; padding: 0; }

From here: http://view.jquerymobile.com/master/demos/examples/maps/geolocation.php

Upvotes: 0

Dmitry S.
Dmitry S.

Reputation: 8503

The solution for my mobile website was to set styles position:absolute; width:100%; height:100%; for the canvas div. You can use the top and bottom styles to leave space for toolbars.

Upvotes: 23

Related Questions