1

I'm confused about the following:

I'm working on a module that creates a interactive Google Maps background, I've added the Javascript to my module ( which is run at hook_init), but when I try to load the page, the maps background doesn't show up.

Before I decided to create a module, I used the same Javascript code which was called on page load through the header-section (through script>src) in my page.tpl.php file, and everything worked as it was supposed to.

So I'm suspecting that, because I call the module with hook_init, my Javascript cannot show up because the API key hasn't yet been validated/declared by the page.

What's the best way to tackle this problem? Can I declare the API key in my .module file? or even my module.js file? Or should I change the hooks to something else, in that case, is there a hook that lets me call the module.js file after the head of my page is loaded?

<?php function maps_markers_init() {
$markerAantal = 0;
$latitudes = db_query('SELECT field_latitude_value FROM {field_data_field_latitude}')->fetchCol();
$longitudes = db_query('SELECT field_longitude_value FROM {field_data_field_longitude}')->fetchCol();

json_encode($latitudes);
echo "<br>";
json_encode($longitudes);
};

And to load the Javascript:

function maps_markers_page_build(){
drupal_add_js(drupal_get_path('module', 'maps_markers') .'/maps_markers.js');
};
3
  • It would be really useful to see the code to see where things might be going wrong - it sounds like it should be a relatively small module, are you able to edit the code into the question?
    – Clive
    Apr 6, 2015 at 14:46
  • Did this get sorted with your more recent question?
    – Clive
    Apr 6, 2015 at 18:57
  • It kinda did! Eventually I ended up placing the .JS file back into my theme folder. I do, however, still have the question regarding how to call a module.JS file with a hook so it loads as if it is called in the head section of the page.tpl.php file. Apr 6, 2015 at 18:59

1 Answer 1

3

Eventually things got fixed, learned that you can just add a .js file into your module with the HOOK_init() since they get stored to use at a later stage.

What obviously was causing the problem, was the wrong declaration of the API key, this was fixed by adding the line:

drupal_add_js("https://maps.googleapis.com/maps/api/js?key=[MY API KEY]", "external");

to the document. After that, I could call the .JS file with:

drupal_add_js(drupal_get_path('module', 'maps_markers') .'/maps_markers.js');

More information on how everything was fixed can be found here: Module conflicting with JQuery

Your Answer

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

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