views:

32

answers:

2

I want to load google maps dynamically so I only load them when needed but I keep getting the error google is not defined here is my code

  $(document).ready(function(){
if($('.geotags').length > 0){
      $("head").append('<script type="text/javascript" src="http://www.google.com/jsapi?key=keytogooglemapsapi"&gt;&lt;/script&gt;');
    google.load("maps", "2.x", {"other_params":"sensor=false",'callback':build_gmaps});
}
  }

EDIT add more code to show where I would like to do this

+1  A: 

google has its own loader in their jsapi script that you can use to load maps api. and if you want to delay-load jsapi itself i would try jquery's getScript.

akonsu
sure would like to see the documentation for that I must have overlooked it somewhere
mcgrailm
i assume you are using version 2: http://code.google.com/apis/maps/documentation/javascript/v2/basics.html
akonsu
oh, and if you want to delay load jsapi itself i would try jquery's getScript
akonsu
ahh yes that shows you how load the google maps while your page is loading but not after the page is loaded
mcgrailm
Mike, I misunderstood your question at first. sorry. i would use jquery to load the loader and then load maps using the loader :)
akonsu
not your fault I wasn't clear enough getscript worked thank you
mcgrailm
could you update your answer to show that getscript is the way to go
mcgrailm
i was thinking about your question and i think that i would do it a bit differently. i would use google's loader to load both the maps and jquery (from the google's cache). i suspect loader is lightweight. that is why they use it to load everything else.
akonsu
A: 

This documentation explains how to delay/dynamic load the maps api: http://code.google.com/apis/ajax/documentation/#Dynamic

Ossama