I found that module extremely useful but I need a new feature that I'm trying to develop before proposing any patch to be considered by the community. What I need is placing the address locator and map of that widget inside a block and then saving the latitude and longitude as session variables.

I'm not succeding and I would appreaciate any help, I'm just trying to mimic the functions I see on the code but it looks that something is happening on a JS level, is like it doesn't recognize the map wrapper and then it doesn't connect with Google Maps.

First of all I've added on "Geolocation Googlemaps" module the block hooks:

<?php
function geolocation_googlemaps_block_info() {
  $blocks = array();
  $blocks['geolocation_googlemaps_block'] = array(
    'info' => 'Dynamic Geolocation Googlemaps Block',
    'cache' => DRUPAL_NO_CACHE,
  );
  return $blocks;
}

function geolocation_googlemaps_block_view($delta = '') {
  $form = drupal_get_form('geolocation_googlemaps_form', $contexts);
  switch ($delta) {
    case 'geolocation_googlemaps_block':
      return array(
        'subject' => 'Geolocation Googlemaps',
        'content' => $form,
      );
      break;
  }
}
?>

This code is working and the block is displayed. Inside I call a form, and I've duplicated the proposed form function for a CCK widget on a more general way commenting and hidding code I've thought that was not necessary:

<?php
function geolocation_googlemaps_form($form) {
  //$id = $instance['id'] . '-' . $delta;
  $id = '1-1';
  
  //$lat_value = isset($items[$delta]['lat']) ? $items[$delta]['lat'] : NULL;
  //$lng_value = isset($items[$delta]['lng']) ? $items[$delta]['lng'] : NULL;
  $lat_value = '';
  $lng_value = '';
  /*$element += array(
    '#delta' => $delta,
  );*/
        
  $form['address'] = array(
    '#type' => 'item',
    '#title' => $form['#title'],
    '#prefix' => '<div id="geolocation-address-' . $id . '" class="geolocation-address">',
    '#suffix' => '</div>',
  );
  $form['address']['field'] = array(
    '#type' => 'textfield',
    '#maxlength' => 120,
  );
  $form['address']['geocode'] = array(
    '#prefix' => '<span id="geolocation-address-geocode-' . $id . '" class="geolocation-address-geocode">',
    '#suffix' => '</span>',
    '#markup' => t('Get location'),
  );
  $form['help'] = array(
    '#prefix' => '<div id="geolocation-help-' . $id . '" class="geolocation-help">',
    '#suffix' => '</div>',
    '#markup' => t('Enter an address / location in the textfield or you can also click on the map to set a marker'),
  );
  $form['googlemap'] = array(
    '#prefix' => '<div id="geolocation-map-' . $id . '" class="geolocation-map" style="width:100%;height:400px;">',
    '#suffix' => '</div>',
  );
  // Presentational item.
  $form['latitem'] = array(
    '#type' => 'item',
    '#title' => t('Latitude:'),
    '#prefix' => '<div id="geolocation-lat-item-' . $id . '" class="geolocation-lat-item">',
    '#suffix' => '</div>',
    '#markup' => '<span class="geolocation-lat-item-value">' . $lat_value . '</span>',
  );
  $form['lat'] = array(
    '#type' => 'hidden',
    '#prefix' => '<div id="geolocation-lat-' . $id . '" class="geolocation-lat">',
    '#suffix' => '</div>',
    '#default_value' => $lat_value,
  );
  // Presentational item.
  $form['lngitem'] = array(
    '#type' => 'item',
    '#title' => t('Longitude:'),
    '#prefix' => '<div id="geolocation-lng-item-' . $id . '" class="geolocation-lng-item">',
    '#suffix' => '</div>',
    '#markup' => '<span class="geolocation-lat-item-value">' . $lat_value . '</span>',
    '#required' => $instance['required'],
  );
  $form['lng'] = array(
    '#type' => 'hidden',
    '#prefix' => '<div id="geolocation-lng-' . $id . '" class="geolocation-lng">',
    '#suffix' => '</div>',
    '#default_value' => $lng_value,
  );
  $form['remove'] = array(
    '#prefix' => '<div id="geolocation-remove-' . $id . '" class="geolocation-remove"><span>',
    '#suffix' => '</span></div>',
    '#markup' => t('Remove'),
  );

  // Attach CSS and JS files via FAPI '#attached'.
  $form['googlemap']['#attached']['css'][] = drupal_get_path('module', 'geolocation_googlemaps') . '/geolocation_googlemaps.css';
  $form['googlemap']['#attached']['js'][] = array(
    'data' => 'http://maps.google.com/maps/api/js?sensor=false',
    'type' => 'external',
  );
  $form['googlemap']['#attached']['js'][] = array(
    'data' => 'http://www.google.com/jsapi',
    'type' => 'external',
  );
  $form['googlemap']['#attached']['js'][] = array(
    'data' => drupal_get_path('module', 'geolocation_googlemaps') . '/geolocation_googlemaps_widget.js',
    'type' => 'file',
    'scope' => 'footer',
  );

  // Make defaults available as javascript settings. In JS files use:
  // Drupal.settings.mapDefaults.lat
  $map_defaults_lat = ($lat_value) ? $lat_value : '';
  $map_defaults_lng = ($lng_value) ? $lng_value : '';
  $map_defaults = array(
    $id => array(
      'lat' => $map_defaults_lat,
      'lng' => $map_defaults_lng,
    ),
  );
  $data = array(
    'defaults' => $map_defaults,
    /*'settings' => $instance['widget']['settings'],*/
  );
  $form['googlemap']['#attached']['js'][] = array(
    'data' => array('geolocation' => $data),
    'type' => 'setting',
  );

  return $form;
}
?>

As you can see on the attached picture the block displays the widtget form but no maps is displayed, and no location is returned, there is no connection with Google Maps.

What I don't understand is why is not painting the map and connecting with Google Maps? It looks to me that the structure of the form is correct but it looks that there is something on the JS that doesn't allow to connect to Google. Why it works as a widget and not as a block, when it looks more simple to me? Anyone could help me to find what is missing?

Comments

jorditr’s picture

Issue summary: View changes

Small correction

jorditr’s picture

Issue summary: View changes

Small corrections

jorditr’s picture

Issue summary: View changes

Another correction

jorditr’s picture

Issue summary: View changes

Define better the final question.

christianadamski’s picture

Component: Geolocation field » Geolocation Field / Backend
Status: Active » Closed (won't fix)

Closing all 7.x issues. It's time.