Reputation: 7788
I started writing my mobile site and I encountered the following problem:
When I first load the index page, and click on maps page, then (top left) occupied by google maps. But if I load maps page
directly
, it loads correctly:
Again: if I go directly to page with maps, it works great!
Here is the fiddle: http://jsfiddle.net/ggrEH/7/
Upvotes: 0
Views: 968
Reputation: 7197
In jQuery Mobile, by default the pages are loaded through AJAX. That's the difference between opening the page directly and through navigation. It is recommended to use a jQuery Mobile event to initialize the map.
I've modified your example:
<!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>
UPDATED:
Example based on the provided 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>
I hope this helps.
Upvotes: 1