Using Google Maps JavaScript API v3 with jQuery Mobile

Written by on April 8, 2015

Using Google Maps JavaScript API v3 with jQuery Mobile

First let us talk about how we can use Gmap v3 API inside a classic web application, it will give us a good perspective when we switch back to jQuery Mobile. At the beginning of a map craze, an iframe was a standard delivery solution. Unfortunately, as time went by it become an outdated delivery source, if anything an obstacle.

The new JavaScript Maps API Version 3 was built from the ground up by Google to offer a clean, fast and powerful maps application development platform for both desktop Web browsers and mobile devices. The v3 API lets developers embed Google Maps in their own Web pages and is specially designed to be faster and more applicable to mobile devices, as well as traditional desktop browser applications, according to Google.

Unlike v2 API which worked on IE6+, Firefox 2.0+, older versions of Chrome v3 API will work on every HTML5 supported browser ranging from IE8+, Firefox 3.0 + up to iOS, Android and BlackBerry 6+ browsers. Which makes it an excellent solution for jQuery Mobile applications. But let me first show you how it works in a classic web page.

Note: If this tutorial was helpful, you need further clarification, something is not working or you have a request for another Ionic post? Furthermore, leave me a comment below if you don't like something about this blog, if something is bugging you, don't like how I'm doing stuff here. Feel free to comment below, subscribe to my blog, mail me to Thanks and have a nice day!

Table of Contents

[spoiler title=” Click here if you want to see solutions to other jQuery Mobile problems, sooner or later you’ll suffer from one.”]


Classic solution


Embedded working example

Classic solution

As you can see, it is an extremely easy solution. It uses vanilla javascript to initialize and display the map. A map is made off numerous canvas elements, and it is fully responsive.


<!DOCTYPE html>
        <title>Google Maps JavaScript API v3 Example: Map Simple</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"/>
        <meta charset="utf-8"/>
            html, body, #map_canvas {
            margin: 0;
            padding: 0;
            height: 100%;
        <script src=""></script>
            var map;
            function initialize() {
            var mapOptions = {
            zoom: 8,
            center: new google.maps.LatLng(-34.397, 150.644),
            mapTypeId: google.maps.MapTypeId.ROADMAP
            map = new google.maps.Map(document.getElementById('map_canvas'),
            google.maps.event.addDomListener(window, 'load', initialize);
        <div id="map_canvas"></div>

Map container can change dynamically, at the same time map will automatically resize to fill a newly available space.

jQuery Mobile Solution

Combine jQuery Mobile and v3 API and everything becomes a little bit complicated. Unlike classic web development jQuery Mobile, pages are responsive by its nature which means page dimensions will become a problem here. During the jQuery Mobile page initialization, a page is constantly changing and resizing. Basically page height can be successfully calculated only during the pageshow event.

For those who still don’t have much experience with jQuery Mobile, pageshow event is a last state to be called during the page initialization. It’s a jQuery Mobile alternative to window ready event. In order to successfully initialize v3 API, we must do it after the page is visible, which somewhat brakes its usefulness.

As always there’s a solution to every problem. If we have a good idea how our page is going to look, we can force the content div to resize according to available space even before pageshow event. We can do this using this CSS rules:

#content {
    padding: 0;
    position : absolute !important; 
    top : 40px !important;  
    right : 0; 
    bottom : 40px !important;  
    left : 0 !important;     

This CSS will remove classic content padding, while absolutely positioned content div will cover available space, giving some space to page header. The bottom rule can be a changed to 40px in case footer is also needed.

One last thing, because map loading is an asynchronous process we need to include a small timeout, without it map would load only partially.


Continue Reading


45 thoughts on “Using Google Maps JavaScript API v3 with jQuery Mobile”

  1. Thanks for all your tutorials! They help so much.

    I have a question: is it wrong to use Google maps in a multi-HTML template? I have a dynamic page with a map that produces markers. Since JQM loads pages in the DOM of the initial page, it’s possible to have several maps within the DOM.

    • You can have as much pages containing maps as you want. There only one problem I can think off, Google Maps API V3 cant successfully preinitialize map in non-active pages. So when other map pages are shown you will always see a brief loading flash. Also take care that div’s containing your maps have unique id’s.

      If you also don’t like map preinitialization problem you can take a look at other map frameworks like Leaflet. It works just fine with jQuery Mobile and unlike GMap API V3 this one don’t suffer from page height problem. More information can be found here:

  2. I have a very similar problem to the one that you discuss, but unfortunately your solution doesn’t work for me. I’m hoping that you might know how to solve my issue.

    I have a jquery mobile site that has two pages. The map needs to reside on the second page, but I want to initialize it (and then get its bounds) it upon pageinit of the index page.

    It should come as no surprise to you that the google map on the second page is sized incorrectly. Here is a fiddle showing the problem: I’ve tried using css to set the size of the map_canvas, but I haven’t had any luck.

    What do you think? Is it possible??

    • Unfortunately I don’t know a solution for this problem. It can be fixed partially, if you initialize map in a first page you will need to trigger map resize during the second page pageshow event. This is the closest solution that I know.

      Here’s a working example:

      In this example map can be used even before second page is shown. But there’s one downside, when second page is shown map will resize to cover available space. This alone is jQuery Mobile problem
      and only way to solve it requires second page to be enhanced at the app load and currently it can’t be done. I had an idea to try to do this manually (manually enhance second page content and add classes) but I never found time to do that.

      I hope this solves your problem.

  3. I really appreciate you taking a look at this problem. Unfortunately, resizing on the second page load doesn’t solve my problem because I need the accurate map bounds before the second page load. My solution (which is specific to my application) is to change the flow so that on the index page I geolocate the user’s location manually establish a set range from that center-point. When the user navigates to the map page I’ll set the map bounds to include all of the range. Not the best approach from a ux perspective, but it will get the job done.

    Thanks again!

    • Thanks for your replay, can you tell me more about your solution? Maybe I can help you make a pure CSS solution. On the other hand jQuery solution is also not bad, I just don’t like using javascript if there’s a CSS solution.

  4. Thanks for article!

    It really works in jsFiddle, but it doesn’t if you copy the code to local html-file. Could you please check that and attach html-file here?

  5. I have used the above solution but the problem i am facing is page is showing as it is loading after that no response. If include the below script the page doesn’t load else the back page appears. If anyone has faced this problem pls let me know:

    $(document).on(‘pageinit’, ‘#index’,function(e,data){
    var minZoomLevel = 12;

    var map = new google.maps.Map(document.getElementById(‘map_canvas’), {
    zoom: minZoomLevel,
    center: new google.maps.LatLng(38.50, -90.50),
    mapTypeId: google.maps.MapTypeId.ROADMAP

  6. I am receiving following error any help pls.

    10-11 06:55:29.580: E/Web Console(2048): Uncaught ReferenceError: google is not defined at file:///android_asset/www/RegisteredUsers.htm:3

  7. Hi Guys,

    Good to come in to this page, I am having a very seriously issue with Gmap, I have 3 pages I want to show my map. only the first page of my map will load fully is I navigate back and click the next page my map size will be weird,..showing half top left. I am using Jquery mobile and phonegap… I am not sure if this was the same case with anyone here, I really need a help to fix this.

    I will appreciate any help.


  8. Hi, a diffrent question, slightly off the current subject.
    Have you ever worked with a KML file? Like importing a KML file over Google Maps to define custom boundaries (polygons). I need some help with colour coding these custom regions dynamically, based on values from a database.

    Any help would be much appreciated.

  9. Hi, i have a different question, slightly off subject.
    Have you ever worked with KML files? Like importing a KML file over Google Maps to display custom boundaries(polygons). I need some help with colour these custom region territories based on asoociated data from a database.

    Any help would be much appreciated.

    • I used them long time ago just to see how everything works, nothing else. Do you have some specific working example I can look? Even better if you can create it inside jsFiddle environment and I will solve your problem.

  10. Hello Dragan,

    It seems your “Map example” download button is broken.

    So far I failed putting a gmap page up and running. All my test work great in chrome / FF but once compiled by PhoneGap, the map div doesn’t show up. A working example for jQuery Mobile & PhoneGap would absolutely rock !


    • Are you sure? It works for me, at least when using desktop Firefox or Chrome. What browser are you using? Also if you can wait until tomorrow I can create you a working Phonegap project. Unfortunately I can only create you Android example.

  11. Hi, am using this with phonegap app. It works fine for android versions below 4.4 , but on android 4.4.2 and 4.4.2 it getCurrentPosition method doesnt work. Fails with [Object PositionError]. Any solution please…

  12. hi how to make a phonegap jquery mobile app truely responsive for all devices. Is media query the only solution?? I have written media query for 3 different resolutions 480×720, 720×1280, 1080×1920. Even the textsize imagesize given in pixels varies from one resolution model to another. There are different phones with different resolutions in market. I dont think writing media query for every single resolutions range is a right solution and its impossible too to get all those models for testing. Please help me get out of this problem. Thanx in advance

  13. Thank you for your constant great work. Especially on behalf of all of us part-timers, this is an invaluable resource.

  14. Why don’t you declare Google maps object as a global variable? This way it will be available to all pages.

    Of course, this solution will not work if you’re accessing other pages (holding maps) with rel=”external”.

    • Hi Dragan, thanks for your reply!
      I have already done this and then I had another issue, the map did not shown either in phonegap neither in browser, without any error in log file. I changed the map’s div width and height from percentages to pixel numbers and the map worked!
      Unfortunately now shows another issue: I try to load Google Maps API via Google AJAX API loader: google.load(“maps”, “3.8”, {“callback”: MyMapFunc, other_params: “sensor=true&language=en”}); and a TypeError occurs at this line of code: “undefined is not a function” and the app stucks there, although I have declared google map api js reference in index.html. Do you have something in mind with this issue please?

  15. Hi,
    I’m new to jquery mobile so please bear with me. I’ve copied the above code and it works perfectly on my laptop but when I load it onto server it momentarily shows then the page goes blank and I get this message on Chrome and Firefox.

    Oops! Something went wrong.
    This page didn’t load Google Maps correctly. See the JavaScript console for technical details.

    Any help will be gratefully received.


Leave a Reply to Gajotres Cancel reply