View source
<?php
define('GMAP_API_VERSION', '2.115');
function gmap_defaults() {
$defaults = array(
'width' => '300px',
'height' => '200px',
'zoom' => 3,
'maxzoom' => 14,
'controltype' => 'Small',
'align' => 'None',
'latlong' => '40,0',
'maptype' => 'Map',
'mtc' => 'standard',
'baselayers' => array(
'Map',
'Satellite',
'Hybrid',
),
'styles' => array(
'line_default' => array(
'0000ff',
5,
45,
'',
0,
0,
),
'poly_default' => array(
'000000',
3,
25,
'ff0000',
45,
),
),
'line_colors' => array(
'#00cc00',
'#ff0000',
'#0000ff',
),
);
$defaults['behavior'] = array();
$m = array();
$behaviors = gmap_module_invoke('behaviors', $m);
foreach ($behaviors as $k => $v) {
$defaults['behavior'][$k] = $v['default'];
}
$defaults = array_merge($defaults, variable_get('gmap_default', array()));
return $defaults;
}
function gmap_module_invoke($op, &$map) {
$return = array();
foreach (module_implements('gmap') as $module) {
$function = $module . '_gmap';
$result = $function($op, $map);
if (isset($result) && is_array($result)) {
$return = array_merge_recursive($return, $result);
}
elseif (isset($result)) {
$return[] = $result;
}
}
return $return;
}
function gmap_gmap($op, &$map) {
switch ($op) {
case 'macro':
return array(
'points' => array(
'multiple' => TRUE,
),
'markers' => array(
'multiple' => TRUE,
),
'feed' => array(
'multiple' => TRUE,
),
'style' => array(
'multiple' => TRUE,
),
);
case 'pre_theme_map':
$path = drupal_get_path('module', 'gmap') . '/js/';
if (isset($map['behavior']['dynmarkers']) && $map['behavior']['dynmarkers'] || !empty($map['markers'])) {
static $header_set = FALSE;
if (!$header_set) {
$header_set = TRUE;
if (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) != FILE_DOWNLOADS_PUBLIC) {
$markerpath = variable_get('gmap_private_markerfile', '');
if (empty($markerpath) || !file_exists($markerpath)) {
drupal_set_message(t('GMap marker file settings are not configured properly for Private download method, markers will not work!'), 'error');
}
else {
drupal_add_js($markerpath, 'module', 'header', FALSE, TRUE, FALSE);
}
}
else {
$markerpath = file_create_path('js');
if (!$markerpath || !file_exists("{$markerpath}/gmap_markers.js")) {
gmap_regenerate_markers();
$markerpath = file_create_path('js');
}
drupal_add_js("{$markerpath}/gmap_markers.js", 'module', 'header', FALSE, TRUE, FALSE);
}
}
drupal_add_js($path . 'icon.js');
drupal_add_js($path . 'marker.js');
$mm = variable_get('gmap_mm_type', 'gmap');
if (function_exists('_gmap_markermanager_override')) {
_gmap_markermanager_override($mm);
}
drupal_add_js($path . $mm . '_marker.js');
}
if (isset($map['behavior']['locpick']) && $map['behavior']['locpick']) {
drupal_add_js($path . 'locpick.js');
}
if (!empty($map['markers']) || !empty($map['lines'])) {
drupal_add_js($path . 'markerloader_static.js');
}
if (!empty($map['shapes'])) {
drupal_add_js($path . 'shapeloader_static.js');
drupal_add_js($path . 'gmap_shapes.js');
}
if (isset($map['feed']) && is_array($map['feed'])) {
drupal_add_js($path . 'markerloader_georss.js');
}
break;
case 'macro_multiple':
return array(
'points',
'markers',
'feed',
'circle',
'rpolygon',
'polygon',
'line',
'style',
);
case 'behaviors':
return array(
'locpick' => array(
'title' => t('Location chooser'),
'default' => FALSE,
'help' => t('Used to activate location choosing using a gmap.'),
'internal' => TRUE,
),
'nodrag' => array(
'title' => t('Disable dragging'),
'default' => FALSE,
'help' => t('Remove the ability for the user to drag the map. If dragging is disabled, keyboard shortcuts are implicitly disabled.'),
),
'nokeyboard' => array(
'title' => t('Disable keyboard'),
'default' => TRUE,
'help' => t('Disable the keyboard shortcuts.'),
),
'nomousezoom' => array(
'title' => t('Disable mousezoom'),
'default' => FALSE,
'help' => t('Disable using the scroll wheel to zoom the map.'),
),
'nocontzoom' => array(
'title' => t('Disable Continuous Zoom'),
'default' => FALSE,
'help' => t('Disable dynamically resizing images while waiting for tiles to load when zooming.'),
),
'autozoom' => array(
'title' => t('Use AutoZoom'),
'default' => FALSE,
'help' => t('Automatically zoom the map to fit all markers when markers are added.'),
),
'dynmarkers' => array(
'title' => t('Unconditionally enable marker interface'),
'default' => FALSE,
'help' => t('Load the marker loader system even if no markers to load are detected. Useful if you are injecting markers from somewhere else.'),
),
'overview' => array(
'title' => t('Enable Overview Map'),
'default' => FALSE,
'help' => t('Enable the "overview map" in the bottom right corner.'),
'previewable' => TRUE,
),
'collapsehack' => array(
'title' => t('Work around bugs when maps appear in collapsible fieldsets'),
'default' => FALSE,
'help' => t('Enabling this will work around some issues that can occur when maps appear inside collapsible fieldsets.'),
),
'scale' => array(
'title' => t('Add scale control to map.'),
'default' => FALSE,
'help' => t('Adds a scale control to the map in the default position.'),
'previewable' => TRUE,
),
'extramarkerevents' => array(
'title' => t('Enable extra marker events.'),
'default' => FALSE,
'help' => t('Used for advanced javascript work, this will enable the <em>mouseovermarker</em>, <em>mouseoutmarker</em>, and <em>dblclickmarker</em> events.'),
'internal' => TRUE,
),
'clickableshapes' => array(
'title' => t('Enable clickable shapes.'),
'default' => FALSE,
'help' => t('Used for advanced javascript work, this will enable the <em>clickshape</em> event.'),
'internal' => TRUE,
),
);
break;
case 'baselayers':
$map['Google']['Map'] = array(
'title' => t('Map: Standard street map.'),
'default' => TRUE,
'help' => t('The standard default street map. Internal name: G_NORMAL_MAP'),
);
$map['Google']['Satellite'] = array(
'title' => t('Satellite: Standard satellite map.'),
'default' => TRUE,
'help' => t('Satellite view without street overlay. Internal name: G_SATELLITE_MAP'),
);
$map['Google']['Hybrid'] = array(
'title' => t('Hybrid: Hybrid satellite map.'),
'default' => TRUE,
'help' => t('Satellite view with street overlay. Internal name: G_HYBRID_MAP'),
);
$map['Google']['Physical'] = array(
'title' => t('Terrain: Physical feature map.'),
'default' => FALSE,
'help' => t('Map with physical data (terrain, vegetation.) Internal name: G_PHYSICAL_MAP'),
);
break;
}
}
function _gmap_doheader() {
static $gmap_initialized = FALSE;
if ($gmap_initialized) {
return;
}
$gmap_path = drupal_get_path('module', 'gmap');
drupal_add_css($gmap_path . '/gmap.css');
drupal_add_js($gmap_path . '/js/gmap.js');
$mm = variable_get('gmap_mm_type', 'gmap');
$mms = variable_get('gmap_markermanager', array());
if (empty($mms[$mm])) {
$mms[$mm] = array();
}
if (function_exists('_gmap_markermanager_override')) {
_gmap_markermanager_override($mm, $mms);
}
if ($mm == 'clusterer' || $mm == 'clustermarker') {
drupal_add_js($gmap_path . '/js/icon.js');
}
if (isset($mms[$mm]['filename'])) {
drupal_add_js($gmap_path . '/thirdparty/' . $mms[$mm]['filename']);
}
drupal_add_js($gmap_path . '/js/marker.js');
drupal_add_js($gmap_path . '/js/' . $mm . '_marker.js');
drupal_add_js(array(
'gmap_markermanager' => $mms[$mm],
), 'setting');
drupal_add_js($gmap_path . '/js/poly.js');
global $locale;
$query = array(
'file' => 'api',
'v' => variable_get('gmap_api_version', GMAP_API_VERSION),
'key' => gmap_get_key(),
'hl' => $locale,
);
drupal_set_html_head('<script src="' . check_url(url('http://maps.google.com/maps', drupal_query_string_encode($query))) . '" type="text/javascript"></script>');
$gmap_initialized = TRUE;
}
function gmap_parse_macro($instring, $ver = 2) {
require_once drupal_get_path('module', 'gmap') . '/gmap_parse_macro.inc';
return _gmap_parse_macro($instring, $ver);
}
function theme_gmap_marker_popup($label) {
return $label;
}
function gmap_set_location($map, &$form, $fields) {
static $ctr = 0;
$ctr++;
if (!is_array($map)) {
$map = array_merge(gmap_defaults(), gmap_parse_macro($map));
}
$id = 'loc' . $ctr;
$map['id'] = $id;
$map['behavior']['locpick'] = TRUE;
$element = array(
'#type' => 'gmap',
'#map' => $map['id'],
'#settings' => $map,
);
$form[$fields['latitude']]['#map'] = $id;
gmap_widget_setup($form[$fields['latitude']], 'locpick_latitude');
$form[$fields['longitude']]['#map'] = $id;
gmap_widget_setup($form[$fields['longitude']], 'locpick_longitude');
if (isset($fields['address'])) {
$form[$fields['address']]['#map'] = $id;
gmap_widget_setup($form[$fields['address']], 'locpick_address');
}
return theme('gmap', $element);
}
function _gmap_prepare($intext) {
$out = FALSE;
$matches = array();
preg_match_all('/\\[gmap([^\\[\\]]+ )* \\] /x', $intext, $matches);
$i = 0;
while (isset($matches[1][$i])) {
$out[0][$i] = $matches[0][$i];
if ($matches[1][$i][0] == '1') {
$ver = 1;
$matches[1][$i] = substr($matches[0][$i], 1);
}
else {
$ver = 2;
}
$map = array(
'#settings' => gmap_parse_macro($matches[1][$i], $ver),
);
$out[1][$i] = theme('gmap', $map);
$i++;
}
return $out;
}
function gmap_todim($instring) {
if (!is_string($instring)) {
return FALSE;
}
$s = strtolower(trim($instring));
$matches = array();
if (preg_match('/^([\\d.]+)\\s*(em|ex|px|in|cm|mm|pt|pc|%)$/', $s, $matches)) {
return $matches[1] . $matches[2];
}
else {
return FALSE;
}
}
function gmap_dimension_validate(&$elem) {
$value = gmap_todim($elem['#value']);
if ($value) {
form_set_value($elem, $value);
}
else {
form_error($elem, t('The specified value is not a valid CSS dimension.'));
}
}
function gmap_filter($op, $delta = 0, $format = -1, $text = '') {
switch ($op) {
case 'list':
return array(
0 => t('GMap macro expander'),
);
case 'name':
return t('Google map filter');
case 'description':
return t('Converts a Google Map macro into the HTML required for inserting a Google Map.');
case 'process':
$gmaps = _gmap_prepare($text);
if ($gmaps) {
return str_replace($gmaps[0], $gmaps[1], $text);
}
else {
return $text;
}
case 'prepare':
return $text;
case 'no cache':
return TRUE;
}
}
function gmap_filter_tips($delta, $format, $long = FALSE) {
if (user_access('create gmap macro')) {
return t('Insert Google Map macro.') . '<a href="' . url('map/macro') . '" target="_blank" >' . t('Create a macro') . '</a>';
}
else {
return t('Insert Google Map macro.');
}
}
function gmap_menu($may_cache) {
if ($may_cache) {
$items = array();
$items[] = array(
'path' => 'admin/settings/gmap',
'title' => t('GMap'),
'description' => t('Configure GMap settings'),
'callback' => 'drupal_get_form',
'callback arguments' => 'gmap_admin_settings',
'access' => user_access('administer site configuration'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
}
function gmap_regenerate_markers() {
$contents = '';
$jspath = file_create_path('js');
file_check_directory($jspath, FILE_CREATE_DIRECTORY);
$contents .= "// GMap marker image data.\n";
$contents .= "Drupal.gmap.iconpath = " . drupal_to_js(base_path() . variable_get('gmap_markerfiles', drupal_get_path('module', 'gmap') . '/markers')) . ";\n";
$contents .= "Drupal.gmap.icondata = " . drupal_to_js(gmap_get_icondata(TRUE)) . ";\n";
file_save_data($contents, "{$jspath}/gmap_markers.js", FILE_EXISTS_REPLACE);
gmap_get_marker_titles(TRUE);
}
function gmap_admin_settings() {
require_once drupal_get_path('module', 'gmap') . '/gmap_settings_ui.inc';
return _gmap_admin_settings();
}
function gmap_elements() {
return array(
'gmap' => array(
'#input' => FALSE,
'#settings' => array_merge(gmap_defaults(), array(
'points' => array(),
'pointsOverlays' => array(),
'lines' => array(),
)),
),
'gmap_macrotext' => array(
'#input' => TRUE,
'#gmap_newtype' => 'textarea',
'#theme' => 'gmap_macrotext',
'#process' => array(
'process_gmap_control' => array(),
),
),
'gmap_overlay_edit' => array(
'#input' => TRUE,
'#process' => array(
'process_gmap_overlay_edit' => array(),
),
),
'gmap_style' => array(
'#input' => TRUE,
'#tree' => TRUE,
'#gmap_style_type' => 'poly',
'#process' => array(
'process_gmap_style' => array(),
),
),
'gmap_address' => array(
'#input' => TRUE,
'#process' => array(
'process_gmap_address' => array(),
),
),
'gmap_align' => array(
'#input' => TRUE,
'#process' => array(
'process_gmap_align' => array(),
),
),
'gmap_latitude' => array(
'#input' => TRUE,
'#gmap_newtype' => 'textfield',
'#process' => array(
'process_gmap_control' => array(),
),
),
'gmap_longitude' => array(
'#input' => TRUE,
'#gmap_newtype' => 'textfield',
'#process' => array(
'process_gmap_control' => array(),
),
),
'gmap_latlon' => array(
'#input' => TRUE,
'#gmap_newtype' => 'textfield',
'#process' => array(
'process_gmap_control' => array(),
),
),
'gmap_markerchooser' => array(
'#input' => TRUE,
'#process' => array(
'process_gmap_markerchooser' => array(),
),
),
'gmap_dimension' => array(
'#input' => TRUE,
'#gmap_newtype' => 'textfield',
'#process' => array(
'process_gmap_control' => array(),
),
'#validate' => array(
'gmap_dimension_validate' => array(),
),
),
);
}
function process_gmap_control($element, $edit) {
$control = substr($element['#type'], 5);
$element['#type'] = $element['#gmap_newtype'];
unset($element['#gmap_newtype']);
gmap_widget_setup($element, $control);
unset($element['#input']);
$element += _element_info($element['#type']);
return $element;
}
function process_gmap_style($element) {
$isline = $element['#gmap_style_type'] == 'line';
$element[0] = array(
'#type' => 'textfield',
'#size' => 6,
'#maxlength' => 6,
'#field_prefix' => '#',
'#title' => t('Stroke color'),
'#value' => $element['#value'][0],
'#attributes' => array(
'class' => 'gmap_style',
),
);
$element[1] = array(
'#type' => 'textfield',
'#title' => t('Stroke weight'),
'#description' => t('Thickness of line, in pixels.'),
'#size' => 3,
'#maxlength' => 3,
'#field_suffix' => t('px'),
'#value' => $element['#value'][1],
'#attributes' => array(
'class' => 'gmap_style',
),
);
$element[2] = array(
'#type' => 'textfield',
'#title' => t('Stroke opacity'),
'#size' => 3,
'#maxlength' => 3,
'#field_suffix' => '%',
'#value' => $element['#value'][2],
'#attributes' => array(
'class' => 'gmap_style',
),
);
$element[3] = array(
'#type' => 'textfield',
'#title' => t('Fill color'),
'#description' => t('Hex color value for fill color. Example: #<em>00AA33</em>'),
'#size' => 6,
'#maxlength' => 6,
'#field_prefix' => '#',
'#value' => $isline ? '' : $element['#value'][3],
'#disabled' => $isline,
'#attributes' => array(
'class' => 'gmap_style',
),
);
$element[4] = array(
'#type' => 'textfield',
'#title' => t('Fill opacity'),
'#description' => t('Opacity of fill, from 0 to 100%.'),
'#size' => 3,
'#maxlength' => 3,
'#field_suffix' => '%',
'#value' => $isline ? '' : $element['#value'][4],
'#disabled' => $isline,
'#attributes' => array(
'class' => 'gmap_style',
),
);
unset($element['#input']);
$element += _element_info('fieldset');
return $element;
}
function theme_gmap_style($element) {
$element['#value'] = '';
return theme('fieldset', $element, $element['#children']);
}
function process_gmap_overlay_edit($element) {
$element['#type'] = 'fieldset';
if (!isset($element['#title'])) {
$element['#title'] = t('Overlay editor');
}
$element['#tree'] = TRUE;
$element['mapclicktype'] = array(
'#type' => 'select',
'#title' => t('Click map'),
'#map' => $element['#map'],
'#options' => array(
'Points' => t('Points'),
'Lines' => t('Lines'),
'Circles' => t('Circles'),
'GPolygon' => t('Filled Polygons'),
),
);
gmap_widget_setup($element['mapclicktype'], 'overlayedit_mapclicktype');
$element['markerclicktype'] = array(
'#type' => 'select',
'#title' => t('Click marker / segment'),
'#map' => $element['#map'],
'#options' => array(
'Remove' => t('Remove'),
'Edit Info' => t('Edit Info'),
),
);
gmap_widget_setup($element['markerclicktype'], 'overlayedit_markerclicktype');
$element['marker'] = array(
'#type' => 'select',
'#map' => $element['#map'],
'#options' => gmap_get_marker_titles(),
'#title' => t('Marker'),
'#theme' => 'gmap_overlay_edit',
);
gmap_widget_setup($element['marker'], 'overlayedit');
$element['linestyle'] = array(
'#type' => 'gmap_style',
'#title' => t('Line style'),
'#gmap_style_type' => 'line',
'#default_value' => array(
'0000ff',
5,
45,
'',
'',
),
);
gmap_widget_setup($element['linestyle'], 'overlayedit_linestyle', $element['#map']);
$element['linestyle']['linestyle_apply'] = array(
'#tree' => FALSE,
'#type' => 'checkbox',
'#title' => t('Use for new and changed lines'),
'#default_value' => FALSE,
);
gmap_widget_setup($element['linestyle']['linestyle_apply'], 'overlayedit_linestyle_apply', $element['#map']);
$element['polystyle'] = array(
'#type' => 'gmap_style',
'#title' => t('Polygon style'),
'#gmap_style_type' => 'poly',
'#default_value' => array(
'000000',
3,
25,
'ff0000',
45,
),
);
gmap_widget_setup($element['polystyle'], 'overlayedit_polystyle', $element['#map']);
$element['polystyle']['polystyle_apply'] = array(
'#tree' => FALSE,
'#type' => 'checkbox',
'#title' => t('Use for new and changed polygons'),
'#default_value' => FALSE,
);
gmap_widget_setup($element['polystyle']['polystyle_apply'], 'overlayedit_polystyle_apply', $element['#map']);
$element += _element_info('fieldset');
return $element;
}
function process_gmap_align($element) {
$element['#type'] = 'select';
gmap_widget_setup($element, 'align');
$element['#options'] = drupal_map_assoc(array(
'None',
'Right',
'Left',
'Center',
));
$element['#theme'] = 'gmap_align';
$element += _element_info('select');
return $element;
}
function process_gmap_address($element) {
$element['#type'] = 'textfield';
gmap_widget_setup($element, 'address');
$element['#theme'] = 'gmap_address';
$element += _element_info('textfield');
return $element;
}
function process_gmap_markerchooser($element) {
$element['#type'] = 'select';
$element['#options'] = gmap_get_marker_titles();
$element += _element_info('select');
return $element;
}
function theme_gmap_overlay_edit($element) {
$path = drupal_get_path('module', 'gmap');
drupal_add_js($path . '/js/gmap.js');
drupal_add_js($path . '/js/gmap_shapes.js');
drupal_add_js($path . '/js/overlay_edit.js');
return theme('select', $element);
}
function gmap_map_cleanup(&$map) {
$map['zoom'] = (int) $map['zoom'];
if ($map['latlong']) {
$map['latlon'] = $map['latlong'];
unset($map['latlong']);
}
if (isset($map['extent']) && is_string($map['extent'])) {
$tmp = explode(',', $map['extent']);
if (count($tmp) == 4) {
$map['extent'] = array(
array(
$tmp[1],
$tmp[0],
),
array(
$tmp[3],
$tmp[2],
),
);
}
else {
unset($map['extent']);
}
}
if (isset($map['latlon']) && (!isset($map['latitude']) || !isset($map['longitude']))) {
list($map['latitude'], $map['longitude']) = explode(',', $map['latlon']);
}
unset($map['latlon']);
foreach ($map['baselayers'] as $k => $v) {
if (!$v) {
unset($map['baselayers'][$k]);
}
}
}
function theme_gmap_macrotext($element) {
drupal_add_js(drupal_get_path('module', 'gmap') . '/js/macro.js');
drupal_add_js(drupal_get_path('module', 'gmap') . '/js/macrobuilder.js');
return theme('textarea', $element);
}
function theme_gmap_address($element) {
drupal_add_js(drupal_get_path('module', 'gmap') . '/js/address.js');
$element['#autocomplete_path'] = '';
return theme('textfield', $element);
}
function theme_gmap_align($element) {
drupal_add_js(drupal_get_path('module', 'gmap') . '/js/align.js');
$element['#multiple'] = FALSE;
return theme('select', $element);
}
function theme_gmap($element) {
if (gmap_get_key() == '') {
return t('Unable to render map: Google Maps API key is missing.');
}
static $mapids = array();
_gmap_doheader();
if (!isset($element['#settings'])) {
$element = array(
'#settings' => $element,
);
}
$mapid = FALSE;
if (isset($element['#map']) && $element['#map']) {
$mapid = $element['#map'];
}
if (isset($element['#settings']['id'])) {
$mapid = $element['#settings']['id'];
}
if (!$mapid) {
$mapid = gmap_get_auto_mapid();
}
$element['#map'] = $mapid;
gmap_widget_setup($element, 'gmap', $mapid);
if (!$element['#settings']) {
$element['#settings'] = array();
}
$element['#settings']['id'] = $mapid;
$mapdefaults = gmap_defaults();
$map = array_merge($mapdefaults, $element['#settings']);
if (isset($element['#settings']['styles'])) {
$map['styles'] = array_merge($mapdefaults['styles'], $element['#settings']['styles']);
}
gmap_map_cleanup($map);
if (isset($map['markers'])) {
foreach ($map['markers'] as $i => $marker) {
if (isset($marker['text'])) {
$map['markers'][$i]['text'] = '<div class="gmap-popup">' . $marker['text'] . '</div>';
}
}
}
switch (strtolower($map['align'])) {
case 'left':
$element['#attributes']['class'] .= ' gmap-left';
break;
case 'right':
$element['#attributes']['class'] .= ' gmap-right';
break;
case 'center':
case 'centre':
$element['#attributes']['class'] .= ' gmap-center';
}
$style = array();
$style[] = 'width: ' . $map['width'];
$style[] = 'height: ' . $map['height'];
$element['#attributes']['class'] = trim($element['#attributes']['class'] . ' gmap gmap-map gmap-' . $mapid . '-gmap');
$o = '<div style="' . implode('; ', $style) . ';" id="' . $element['#id'] . '"' . drupal_attributes($element['#attributes']) . '>' . t('Javascript is required to view this map.') . '</div>';
gmap_module_invoke('pre_theme_map', $map);
if (isset($mapids[$element['#map']])) {
drupal_set_message(t('Duplicate map detected! GMap does not support multiplexing maps onto one MapID! GMap MapID: %mapid', array(
'%mapid' => $element['#map'],
)), 'error');
return $o;
}
$mapids[$element['#map']] = TRUE;
drupal_add_js(array(
'gmap' => array(
$element['#map'] => $map,
),
), 'setting');
return $o;
}
function gmap_widget_setup(&$element, $type, $map = NULL) {
if (!$map) {
if (isset($element['#map'])) {
$map = $element['#map'];
}
else {
if (isset($element['#settings']['id'])) {
$map = $element['#settings']['id'];
}
}
}
if (!isset($element['#attributes']['class'])) {
$element['#attributes']['class'] = '';
}
$element['#attributes']['class'] = trim(implode(' ', array(
$element['#attributes']['class'],
'gmap-control',
'gmap-' . $type,
)));
$element['#id'] = gmap_get_id($map, $type);
$element['#map'] = $map;
}
function gmap_get_id($map, $type) {
static $serial = array();
if (!isset($serial[$map])) {
$serial[$map] = array();
}
if (!isset($serial[$map][$type])) {
$serial[$map][$type] = -1;
}
$serial[$map][$type]++;
return 'gmap-' . $map . '-' . $type . $serial[$map][$type];
}
function gmap_get_auto_mapid() {
static $auto = 0;
$auto++;
return 'auto' . $auto . 'map';
}
function gmap_get_marker_titles($reset = FALSE) {
static $titles;
if (!$reset) {
if (is_array($titles)) {
return $titles;
}
$cached = cache_get('gmap_marker_titles', 'cache');
if (!empty($cached)) {
$titles = unserialize($cached->data);
if (is_array($titles)) {
return $titles;
}
}
}
require_once drupal_get_path('module', 'gmap') . '/gmap_markerinfo.inc';
$titles = _gmap_get_marker_titles();
cache_set('gmap_marker_titles', 'cache', serialize($titles));
return $titles;
}
function gmap_get_icondata($reset = FALSE) {
static $icons;
if (is_array($icons) && !$reset) {
return $icons;
}
$icons = cache_get('gmap_icondata');
if ($icons) {
$icons = unserialize($icons->data);
}
if ($reset || !$icons) {
require_once drupal_get_path('module', 'gmap') . '/gmap_markerinfo.inc';
$icons = _gmap_get_icondata();
}
cache_set('gmap_icondata', 'cache', serialize($icons));
return $icons;
}
function gmap_decimal($num) {
$num = trim($num);
if (preg_match('/^[+\\-]?((\\d+)|(\\d+\\.\\d*)|(\\d*\\.\\d+))(e[+\\-]?\\d+)?$/', $num) === 1) {
return $num;
}
return (double) $num;
}
function gmap_geocode($address, $tld = 'com') {
$key = gmap_get_key();
$data = drupal_http_request('http://maps.google.' . $tld . '/maps/geo?q=' . drupal_urlencode($address) . '&output=csv&key=' . $key);
if ($data->code == 200) {
$r = explode(',', $data->data);
return array(
'status' => (int) $r[0],
'accuracy' => (int) $r[1],
'latitude' => $r[2],
'longitude' => $r[3],
);
}
return array(
'status' => 500,
);
}
function gmap_simple_map($latitude, $longitude, $markername = '', $info = '', $zoom = 'auto', $width = 'default', $height = 'default', $autoshow = FALSE, $map = array()) {
$settings = array(
'id' => gmap_get_auto_mapid(),
'latitude' => $latitude,
'longitude' => $longitude,
);
if ($zoom != 'default') {
$settings['zoom'] = $zoom;
}
if ($width != 'default') {
$settings['width'] = $width;
}
if ($height != 'default') {
$settings['height'] = $height;
}
$settings['markers'] = array(
array(
'latitude' => $latitude,
'longitude' => $longitude,
'markername' => $markername,
'offset' => 0,
),
);
if (!empty($info)) {
$settings['markers'][0]['text'] = $info;
}
if ($autoshow) {
$settings['markers'][0]['autoclick'] = TRUE;
}
if (!empty($map)) {
$settings = array_merge($settings, $map);
}
return theme('gmap', array(
'#settings' => $settings,
));
}
function gmap_keys_service() {
if (module_exists('keys_api')) {
return array(
'gmap' => array(
'name' => t('Gmap'),
'description' => t('Google Maps API Key'),
),
);
}
elseif (module_exists('keys')) {
$test = array();
if (function_exists('keys_keys_service')) {
$test = keys_keys_service();
}
if (!isset($test['google_maps'])) {
return array(
'google_maps' => array(
'name' => t('Google Maps'),
'description' => t('Google Maps API Key'),
),
);
}
}
}
function gmap_get_key() {
$key = variable_get('googlemap_api_key', '');
if (module_exists('keys_api')) {
$key = keys_api_get_key('gmap', $_SERVER['HTTP_HOST']);
}
elseif (module_exists('keys')) {
$key = keys_get_key('google_maps');
}
return $key;
}