diff --git a/gmap3_tools.inc b/gmap3_tools.inc
index e1c7d8a..d4b261e 100644
--- a/gmap3_tools.inc
+++ b/gmap3_tools.inc
@@ -52,7 +52,7 @@ define('GMAP3_TOOLS_DEFAULT_MARKERS_POSITION_CENTER_ZOOM', 'center zoom');
  *   Gmap configuration array. Check $defaults array for list of all supported
  *   options.
  */
-function gmap3_tools_add_map($options) {
+function gmap3_tools_add_map($options, &$form = NULL) {
   static $init = FALSE;
 
   static $defaults = array(
@@ -124,16 +124,42 @@ function gmap3_tools_add_map($options) {
     if ($api_key = variable_get('gmap3_tools_api_key', NULL)) {
       $lib_url .= '&key=' . $api_key;
     }
-    drupal_add_js($lib_url, 'external', array('scope' => 'footer'));
-
-    drupal_add_js(drupal_get_path('module', 'gmap3_tools') . '/gmap3_tools.js', array('scope' => 'footer'));
+    // Whenever using JS in a form, add it using the #attached property.
+    if ($form) {
+      $form['#attached']['js'] = array(
+        array(
+          'data' => $lib_url,
+          'type' => 'external',
+        ),
+        array(
+          'data' => drupal_get_path('module', 'gmap3_tools') . '/gmap3_tools.js',
+          'scope' => 'footer',
+        ),
+      );
+    }
+    else {
+      drupal_add_js($lib_url, 'external', array('scope' => 'footer'));
+      drupal_add_js(drupal_get_path('module', 'gmap3_tools') . '/gmap3_tools.js', array('scope' => 'footer'));
+    }
   }
 
   // Merge custom map options with defaults.
   $options = gmap3_tools_array_union_recursive($defaults, $options);
 
   // Add map configuration options to js.
-  drupal_add_js(array('gmap3_tools' => array('maps' => array($options))), 'setting');
+  if ($form) {
+    $form['#attached']['js'][] = array(
+      'data' => array(
+        'gmap3_tools' => array(
+          'maps' => array($options)
+        ),
+      ),
+      'type' => 'setting',
+    );
+  }
+  else {
+    drupal_add_js(array('gmap3_tools' => array('maps' => array($options))), 'setting');
+  }
 }
 
 /**
