9

As you see on the below picture, the map only shows up in upper left corner, It is possible to drag it around on the whole screen, but it will jump back to that square and only be displayed there.

The plugin mentioned in Title: SemiOfficial jquery plugin

So I'm wondering how I can fix this?

Below the picture You'll find my html, a list of plugins, and the js code.

Map only shows upper left corner

HTML/JSP: (Should be mentioned this is the 2nd .jsp in my app, so it should hopefully initialize in the pageinit method)

<!DOCTYPE html> 
<html>
    <head> 
        <title>Map</title> 
        <meta name="viewport" content="width=device-width, initial-scale=1"> 
        <link rel="stylesheet" href="js/jquery.mobile-1.1.0.css" />
    </head> 
    <body> 
        <!-- Start of first page -->
        <div data-role="page" id="mapmode" name="mapmode">

            <div data-role="header" id="header" name="header">
                <p>Header</p>
            </div><!-- /header -->
            <div data-role="content" id="content" name="content">   
                <div id="map_canvas" style="width:300px;height:350px"></div>

            </div><!-- /content -->
        </div><!-- /page -->
    </body>
</html>

PLUGINS:

<script type="text/javascript" src="js/jquery-1.7.2.js"></script>
        <script type="text/javascript" src="js/jquery.mobile-1.1.0.js"></script>
        <script type="text/javascript"
                src="http://maps.googleapis.com/maps/api/js?key=_MY_KEY__INSERT_HERE_&sensor=true">
        </script>
        <script type="text/javascript" src="js/jquery.ui.map.js"></script>
        <script type="text/javascript" src="js/cordova-1.9.0.js"></script>
        <script type="text/javascript" src="js/FileManager.js"></script>
        <script type="text/javascript" src="js/LocalAction.js"></script>
        <script type="text/javascript" src="js/Menu.js"></script>
        <script type="text/javascript" src="js/MenuLoader.js"></script>
        <script type="text/javascript" src="js/PageHeader.js"></script>
        <script type="text/javascript" src="js/Mapmode.js"></script>

Mapmode.js:

var mapdata = {
    destination: new google.maps.LatLng(59.3327881, 18.064488100000062)
};

$(document).on("pageinit", "#mapmode", function(event) {
    initMapMode();
    document.getElementById("map_canvas").style.height=$(window).height();
    document.getElementById("map_canvas").style.width=$(window).width();
    //Create the map then make 'displayDirections' request
    $('#map_canvas').gmap({
        'center' : mapdata.destination, 
        'mapTypeControl' : true, 
        'navigationControl' : true,
        'navigationControlOptions' : {
            'position':google.maps.ControlPosition.LEFT_TOP
        }
    })
    .bind('init', function() {
        $('.refresh').trigger('tap');        
    });
});

$('#mapmode').on("pageshow", function() {
    $('#map_canvas').gmap('refresh');
});

function initMapMode(){
    initPageHeader();
    initMapModeContent();
}

function initMapModeContent(){
}

function fadingMsg (locMsg) {
    $("<div class='ui-overlay-shadow ui-body-e ui-corner-all fading-msg'>" + locMsg + "</div>")
    .css({
        "display": "block", 
        "opacity": 0.9, 
        "top": $(window).scrollTop() + 100
    })
    .appendTo( $.mobile.pageContainer )
    .delay( 2200 )
    .fadeOut( 1000, function(){
        $(this).remove();
    });
}

// Request display of directions, requires jquery.ui.map.services.js
var toggleval = true; // used for test case: static locations
$('.refresh').live("tap", function() {
    // START: Tracking location with test lat/long coordinates
    // Toggle between two origins to test refresh, force new route to be calculated
    var position = {};
    if (toggleval) {
        toggleval = false;
        position = {
            coords: {
                latitude: 57.6969943, 
                longitude: 11.9865
            }
        }; // Gothenburg
    } else {
        toggleval = true;
        position = {
            coords: {
                latitude: 58.5365967, 
                longitude: 15.0373319
            }
        }; // Motala
    }
    $('#map_canvas').gmap('displayDirections', 
    {
        'origin' : new google.maps.LatLng(position.coords.latitude, position.coords.longitude), 
        'destination' : mapdata.destination, 
        'travelMode' : google.maps.DirectionsTravelMode.DRIVING
    },
    {
        'panel' : document.getElementById('dir_panel')
    },
    function (result, status) {
        if (status === 'OK') {
            var center = result.routes[0].bounds.getCenter();
            $('#map_canvas').gmap('option', 'center', center);
            $('#map_canvas').gmap('refresh');
        } else {
            alert('Unable to get route');
        }
    }); 
    // END: Tracking location with test lat/long coordinates
    $(this).removeClass($.mobile.activeBtnClass);
    return false;
});

initpageheader function:

function initPageHeader(){
    //TODO getdata with the id(page we are currently on).
    $("#header").html(function(index, originalMarkup) {
        return '<a data-theme="a" data-wrapperels="span" data-iconshadow="true" data-shadow="true" '+
        'data-corners="true" class="ui-btn-left ui-btn ui-btn-up-a ui-shadow ui-btn-corner-all" href="#" '+
        'data-rel="back" data-role="button"><span class="ui-btn-inner ui-btn-corner-all"><span class="ui-btn-text">'+
        '<img src="../images/back.png" alt="back" align="middle" vspace="2"></span></span></a>'+
        '<h1 aria-level="1" role="heading" class="ui-title">'+
        '<img src="../images/main_header.png" alt="logo" align="middle" vspace="2">'+
        '</h1><a data-theme="a" data-wrapperels="span" data-iconshadow="true" data-shadow="true"'+
        'data-corners="true" class="ui-btn-right ui-btn ui-btn-up-a ui-btn-inline ui-shadow ui-btn-corner-all" '+
        'href="#first" data-role="button" data-inline="true"><span class="ui-btn-inner ui-btn-corner-all">'+
        '<span class="ui-btn-text">'+
        '<img src="../images/home.png" alt="picture to take you to the first page" align="middle">'+
        '</span></span></a>';
    });
}
Anders Metnik
  • 6,096
  • 7
  • 40
  • 79
  • We also need the CSS. GoogleMap sometimes can act like that when some parents don't have a relative position. Can you provide a fiddle which has all the required plugins ? – dievardump Jul 26 '12 at 13:14
  • Its almost standard jquerymobile css, but sure ill put up something :) – Anders Metnik Jul 27 '12 at 06:16
  • http://jsfiddle.net/qDe7s/ ... I'm new to fiddle, so don't hang me if this isn't what you wanted :) – Anders Metnik Jul 27 '12 at 06:48
  • I think [this question](http://stackoverflow.com/questions/1428178/problems-with-google-maps-api-v3-jquery-ui-tabs) (second answer) might be relevant. I have had a similar problem before and it was because the containing element did not have it's width and height defined when maps initialized. Maybe something similar is happening here? – smilly92 Jul 29 '12 at 14:07
  • Created a jsfiddle for you that has the bog standard googlemaps, jquery, jquerymobile, and the jquerymobile css in it. Works fine. http://jsfiddle.net/asfR9/ – Jeemusu Jul 30 '12 at 07:35
  • @smilledge If you can tell me how to use it with jquery gmap plugin, i'd be realyl really happy :) – Anders Metnik Jul 30 '12 at 07:40
  • I didn't realise you were using gmap, it wasn't included in your code above. The above code is also missing the `initPageHeader();` function – Jeemusu Jul 30 '12 at 07:59
  • Yeah sorry about that @Jeemusu, both is described and implemented in the Q as of now. – Anders Metnik Jul 30 '12 at 08:15
  • used to have the same problem but on my side turned out to be because of the slow connection eventually it opened did you give it time ? – Patsy Issa Jul 30 '12 at 08:27
  • @PatsyIssa Yes I have given it lots of time :-) and my connection is really fast, else thanks :) – Anders Metnik Jul 30 '12 at 08:51
  • Hmm, it's going to be hard to help you without setting this up in a local testing environment. Try applying the CSS i have in this http://jsfiddle.net/asfR9/1/ . Specifically the `#map_canvas`, `div.ui-content` and `mapmode` ​. – Jeemusu Jul 30 '12 at 10:16
  • @Jeemusu danm, think I forgot to say that mapmode, the page, is in a separate jsp file. So need stuff initialized in pageinit event, but it doesn't work if I do that there :/ (have also tried without, no glory that way either) – Anders Metnik Jul 30 '12 at 10:38

2 Answers2

1

try to refresh the frame of the map with api function

after this line

$('#map_canvas').gmap('refresh');

try this

google.maps.event.trigger(map, "resize");
Paulo Pinto
  • 194
  • 1
  • 8
0

On the container that is holding the google map.

#map_canvas { max-width: none; }

arhea
  • 454
  • 2
  • 5