1

I'm trying to create a module that will make use of Google Maps to display the current places indicated in the search results via markers. The problem that I'm having is that I'm getting the following error when I add the Google maps script:

Uncaught ReferenceError: jQuery is not defined 

The map wouldn't work if I don't include my own version of jQuery, but the error stays there whether or not I include jQuery.

function gmapsnrew_block_view($delta = ''){
    $block['subject'] = 'Google Map Test';
    $block['content'] = '<div id="map-canvas"></div>';
    return $block;
}



drupal_add_css(drupal_get_path('module', 'gmapsnrew') . '/css/gmapsnrew.css');

drupal_add_js('http://maps.googleapis.com/maps/api/js?key=google_key&sensor=false&libraries=places');

drupal_add_js(drupal_get_path('module', 'gmapsnrew') . '/js/load-map.js');

And the load-map.js looks something like this:

(function ($) {
  Drupal.behaviors.gmapsnrew = {

    attach: function(context, settings){    

      console.log(context);
      var base_lat = 51.322377;
      var base_lng = -0.1358427;

      var latlng = new google.maps.LatLng(base_lat, base_lng);
      var options = {
          zoom: 10,
          center: latlng,
          mapTypeId: google.maps.MapTypeId.ROADMAP,
          mapTypeControl: false
      };
      var map = new google.maps.Map(document.getElementById("map-canvas"), options);  
    }
  }

})(jQuery);

Any ideas what I'm doing wrong here? Thanks in advance.

4
  • Adding your own version of jquery? This isnt going to work.
    – Alex Gill
    Jun 17, 2013 at 10:12
  • Are you explicitly including a JQuery version which is not supported by Drupal? In-built supported jQuery versions D6 - 1.3.2, D7 - 1.5.2 Jun 17, 2013 at 10:41
  • like I said above, even when not adding the jQuery library I still get the error. I believe google maps would work only if jquery has been added first.
    – soul
    Jun 17, 2013 at 12:46
  • Your code sample is incomplete. Where is it you are calling drual_add_js() from?
    – rooby
    Jun 17, 2013 at 12:55

1 Answer 1

3

If your code sample is accurate, and you are calling drupal_add_js() from outside a function in a module file, then that will be the problem.

You would want to add your js from within your hook_block_view(). This way it loads after drupal has done all it needs to do with its js and jQuery will exist.

Also, you might not want to put your map initialisation in a behavior, because you would usually only ever want your map to load once, on page load. I don't know all the details of your setup though so I can't know exactly what is best in your case.

0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.