1

I'm using the Google Maps API on my website regarding restaurants. The map loads when a restaurant has been selected. I was initially using the google embed map, but I got some errors (e.g. markers not showing), so I switched to gm API.

How do I use a drupal.setting variable to a javascript? This is my code.

var myCenter = new google.maps.LatLng(Drupal.settings.eatery_geo.address);
function initialize() {
  var mapProp = {
    center:myCenter,
    zoom:16,
    mapTypeId:google.maps.MapTypeId.ROADMAP
  };

  var map = new google.maps.Map(document.getElementById("map-canvas"),mapProp);

  var marker = new google.maps.Marker({
    position:myCenter,
  });

  marker.setMap(map);
}

google.maps.event.addDomListener(window, 'load', initialize);

Where Drupal.settings.eatery_geo.address has the variable?

This doesn't seem to work; it only shows a blank <div>.

4

It gets its value from your PHP code. Here is an example of using drupal_add_js:

//Your module.php
$settings = array(
  'helloWorld' => array(
    'display' => 'alert',
    'message' => 'Hello World!',
  ),
);
drupal_add_js($settings, 'setting');

You can access the message in your JavaScript with Drupal.settings.helloWorld.message.

0

I was receiving the following error:

Warning: Illegal offset type in drupal_add_js() (line 4230 of /var/www/drupal/includes/common.inc).

Changing the word 'settings' to 'setting' in the drupal_add_js() line seemed to fix it for me:

 <?php

 $settings = array(
   'helloWorld' => array(
     'display' => 'alert',
     'message' => 'Hello World!',
   ),
 );
 drupal_add_js(array('my_custom_module_name' => $settings), 'setting');

I put the above code in a custom module, let's call it my_custom_module_name for this example.

Then put the following in my javascript file:

Drupal.behaviors.my_custom_module_name = {
  attach: function(context, settings) {

    // Place your code here.
    // alert(Drupal.settings.my_custom_module_name.helloWorld.message);
    console.log(Drupal.settings.my_custom_module_name.helloWorld.message);

  }
};

Make sure your_theme_name.info references your js file in order for it to load, for example:

scripts[] = js/script.js

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