4

I got a problem with loading the google maps api.

I got my own object with a function that initializes the map, and the google maps api gets loaded via jquery.getscript. but i always get an error message in the callback function:

var MyGMap = {
    GMapScriptURL: "http://maps.google.com/maps?file=api&v=2&async=2&key=",
    Map: null,
    Geocoder: null,
    InitiazlizeMaps: function () {
        if (GBrowserIsCompatible()) {
            this.Map = new GMap2(document.getElementById("map_canvas"));
            this.Map.setCenter(new GLatLng(37.4419, -122.1419), 13);
            this.Geocoder = new GClientGeocoder();
        }
    }
}

$(function(){
    var CurrentKey = "MY_KEY";

    $.getScript(MyGMap.GMapScriptURL + CurrentKey, function () {
        MyGMap.InitiazlizeMaps();
        // throws GMap2 is undefined

    });
});

whats wrong? why is this not running?

1
  • what is th error message you get? Commented Jun 3, 2010 at 12:38

2 Answers 2

6

You've got async=2 in the script URL line which means load the mapping core asynchronously too - you need to wait for it to complete that before you can call InitializeMaps. You can either try dropping the async=2 from the URL, or using Google Map's async and callback instead of the getScript callback function, e.g.

$.getScript(MyGMap.GMapScriptURL + CurrentKey + "&callback=MyGMap.InitializeMaps");
Sign up to request clarification or add additional context in comments.

6 Comments

Just to add on, Dion Almaer mentions the use of the callback on his blog (I was typing out my answer when this answer appeared first so I scrapped mine.)
strange...now i get an error: _mF is not defined where is this coming from?
Oh, sorry, I typoed Initiazlize - I didn't spot you had a z in the middle. Maybe that's it? :-/
yeah, thanks, now i see the Google Logo at least.. but no Map... you know why?
No, sorry - that all looks OK to me now. I'd have guessed zoom level but 13 works fine at maps.google.co.uk/maps?q=37.4419,+-122.1419&z=13
|
0

you can also use the async mode, but you need to provide a callback function.

Here http://lucamanzo-soluzione-software.it/wp/?p=5 you can find a simple jquery plugin showing all the steps to use the async loading and jquery, and building a map with just a few lines of code:

$.gmapstools.init();
$("#my_map_canvas").gmap({lat:37.4221913, lng:-122.08458530000001, draw_marker:true, zoom_level:13});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.