You are here

function ip_geoloc_output_map_multi_location in IP Geolocation Views & Maps 8

Same name and namespace in other branches
  1. 7 ip_geoloc_api.inc \ip_geoloc_output_map_multi_location()

Outputs an HTML div placeholder into which will be injected a Google map.

The locations to be mapped are supplied as an array of location objects, each with lat,long coordinates and optional balloon text.

Note this function will result in the visitor being prompted to share their location.

Parameters

array $locations: array of location objects each containing lat/long pair and optional balloon text.

string $div_id: id of the div placeholder, can be anything as long as it's unique

string $map_options: as a JSON string, .e.g '{"mapTypeId":"roadmap", "zoom":15}'

string $map_style: CSS style applied to the div, e.g "height:250px; width:300px"

string $marker_color: default color used for all locations that haven't had their color overridden, defaults to Google default (i.e. red marker with dot).

bool|string $visitor_marker: FALSE (no marker), TRUE (standard red marker) or a 'RRGGBB' color code

int $center_option: 0: fixed cenrer, must be provided thorugh $map_options) 1: auto-center the map on the first location in the $locations array 2: auto-center the map on the visitor's current location

array $center_latlng: array of length 2 with lat/long coords used as a backup when $visitor_marker is set or $center_option == 2 and location could not be determined or $visitor_location_gps == FALSE

bool $visitor_location_gps: whether the HTML5-style location provider is to be used, if FALSE $center_latlng is used

Return value

string map placeholder div

File

./ip_geoloc_api.inc, line 250
API functions of IP geolocation module

Code

function ip_geoloc_output_map_multi_location($locations, $div_id = 'ip-geoloc-map-multi-locations', $map_options = NULL, $map_style = NULL, $marker_color = NULL, $visitor_marker = TRUE, $center_option = 0, $center_latlng = array(
  0,
  0,
), $visitor_location_gps = TRUE) {
  drupal_add_js(IP_GEOLOC_GOOGLE_MAPS, array(
    'type' => 'external',
    'group' => JS_LIBRARY,
  ));
  drupal_add_js(drupal_get_path('module', 'ip_geoloc') . '/js/ip_geoloc_gmap_multi_loc.js');
  if (!isset($map_options)) {
    $map_options = IP_GEOLOC_RECENT_VISITORS_MAP_OPTIONS;
  }
  $marker_directory = file_create_url(ip_geoloc_marker_directory());
  $marker_dimensions = explode('x', ip_geoloc_marker_dimensions());
  $marker_width = (int) $marker_dimensions[0];
  $marker_height = (int) $marker_dimensions[1];
  switch (\Drupal::state()
    ->get('ip_geoloc_marker_anchor_pos', 'bottom')) {
    case 'top':
      $marker_anchor = 0;
      break;
    case 'middle':
      $marker_anchor = (int) (($marker_height + 1) / 2);
      break;
    default:
      $marker_anchor = $marker_height;
  }
  ip_geoloc_debug(t('IPGV&M: passing the following to Google Maps:'));
  ip_geoloc_debug(t('- map options %options:', array(
    '%options' => $map_options,
  )));
  ip_geoloc_debug(t('- center option: @option', array(
    '@option' => $center_option,
  )));
  ip_geoloc_debug(t('- visitor marker: %marker', array(
    '%marker' => $visitor_marker,
  )));
  ip_geoloc_debug(t('- use GPS: @gps', array(
    '@gps' => (bool) $visitor_location_gps,
  )));
  ip_geoloc_debug(t('- visitor location fallback: (@lat, @lng)', array(
    '@lat' => empty($center_latlng[0]) ? '-' : $center_latlng[0],
    '@lng' => empty($center_latlng[1]) ? '-' : $center_latlng[1],
  )));
  ip_geoloc_debug(t('- marker directory : %dir', array(
    '%dir' => $marker_directory,
  )));
  ip_geoloc_debug(t('- marker dimensions : w@w x h@h px, anchor: @a px', array(
    '@w' => $marker_width,
    '@h' => $marker_height,
    '@a' => $marker_anchor,
  )));
  ip_geoloc_debug(t('- @count locations found', array(
    '@count' => count($locations),
  )));

  // Locations transferred to JS via the settings array aren't refreshed in
  // AJAX contexts. That's why we are doing it this messy way. @todo: refactor.
  print "\n<script>\nif (typeof(ip_geoloc_locations) === 'undefined') {\n ip_geoloc_locations = new Array();\n}\n";
  print "ip_geoloc_locations['{$div_id}'] = [\n";
  $illegal_chars = array(
    "\n",
    "\r",
  );
  foreach ($locations as $location) {
    if (isset($location->type) && $location->type != 'point') {
      continue;
    }

    // Balloon text must not have newlines, use <br/> instead.
    $balloon_text = NULL;
    if (!empty($location->balloon_text)) {
      $balloon_text = str_replace($illegal_chars, ' ', $location->balloon_text);
      $balloon_text = addslashes($balloon_text);
    }
    $output = '{' . '"type":"point"' . (empty($location->marker_color) ? '' : ',"marker_color":"' . $location->marker_color . '"') . (empty($balloon_text) ? '' : ',"balloon_text":"' . $balloon_text . '"') . (empty($location->open) ? '' : ',"open":1') . ',"latitude":' . $location->latitude . ',"longitude":' . $location->longitude . "},\n";
    print $output;
    $color = empty($location->marker_color) ? t('default') . " [{$marker_color}]" : $location->marker_color;
    $coords = isset($location->latitude) ? $location->latitude . ', ' . $location->longitude : '';
    $msg = '- ' . t('marker') . " {$color} ({$coords})<br/>{$balloon_text}<br/>";
    ip_geoloc_debug($msg);
  }
  print "];\n</script>\n";
  $settings = array(
    'ip_geoloc_multi_location_map_div' => $div_id,
    'ip_geoloc_multi_location_map_options' => drupal_json_decode($map_options),
    'ip_geoloc_multi_location_center_option' => (int) $center_option,
    'ip_geoloc_multi_location_center_latlng' => $center_latlng,
    'ip_geoloc_multi_location_visitor_marker' => $visitor_marker,
    'ip_geoloc_multi_location_visitor_location_gps' => $visitor_location_gps,
    'ip_geoloc_multi_location_marker_directory' => $marker_directory,
    'ip_geoloc_multi_location_marker_width' => (int) $marker_width,
    'ip_geoloc_multi_location_marker_height' => (int) $marker_height,
    'ip_geoloc_multi_location_marker_anchor' => (int) $marker_anchor,
    'ip_geoloc_multi_location_marker_default_color' => $marker_color,
  );
  drupal_add_js(array(
    $settings,
  ), 'setting');
  if (!isset($map_style)) {
    $map_style = IP_GEOLOC_MAP_DIV_DEFAULT_STYLE;
  }
  $map_placeholder = "<div id='{$div_id}'" . (empty($map_style) ? '' : " style='{$map_style}'") . '></div>';
  return $map_placeholder;
}