Skip to content
gmap.module 44.8 KiB
Newer Older
webgeer's avatar
webgeer committed
<?php
/* $Id$ */

/**
 * @file
 * GMap Filters is a module to include Google Map in a module
 *
 * GMap filter allows the insertion of a googlemap in a module.  It has
 * a page to creat a macro and then a filter to convet the macro into the
 * html and javascript code required to insert a google map.
 */


define('GMAP_LOAD_METHOD', variable_get('gmap_method','Static'));
define('GMAP_WIDTH', variable_get('gmap_default_width', '300px'));
define('GMAP_HEIGHT', variable_get('gmap_default_height', '200px'));
webgeer's avatar
webgeer committed
define('GMAP_ZOOM', variable_get('gmap_default_zoom', 3));
define('GMAP_CONTROL', variable_get('gmap_default_control', 'Small'));
//define('GMAP_ALIGNMENT', variable_get('gmap_default_allignment', ???));
webgeer's avatar
webgeer committed
define('GMAP_LONGLAT', variable_get('gmap_default_latlong', '40,0'));
define('GMAP_TYPE', variable_get('gmap_default_type', 'Map'));
define('GMAP_LINECOLOR1', variable_get('gmap_default_line1_color', '#00cc00'));
define('GMAP_LINECOLOR2', variable_get('gmap_default_line2_color', '#ff0000'));
define('GMAP_LINECOLOR3', variable_get('gmap_default_line3_color', '#0000ff'));
webgeer's avatar
webgeer committed
//define('GMAP_XMAPS', variable_get('gmap_xmaps',0));
define ('GMAP_XMAPS',0); //xmaps not currently compatible with v2 of google maps api
define('GMAP_XMAPS_SCRIPT', variable_get('gmap_xmaps_script','misc/xmaps.1c.js'));
define('GMAP_CLUSTER', variable_get('gmap_cluster',0));
define('GMAP_API_V', '2.55a');

webgeer's avatar
webgeer committed
/**
webgeer's avatar
webgeer committed
 *
 */

function gmap_help($section) {
  switch ($section) {
    case 'admin/modules#description':
      return t('Filter to allow insertion of a google map into a node');
  }
}

webgeer's avatar
webgeer committed
function gmap_perm() {
webgeer's avatar
webgeer committed
}

/**
 *
 * Returns the html required to insert a map from a gmap associative array.
 *
 * @param $gmap
 * An associative array with the following variables set:
 *
 *  id - the id of the map every map on a page must have a unique id
 *  width - width of the map
 *  height - height of the map
webgeer's avatar
webgeer committed
 *  center - a string of the latitude, longitude of the centre of the map
webgeer's avatar
webgeer committed
 *  zoom - the zoom factor of the google map
 *  align - the alignment of the map 'right', 'left' or 'center'
 *  control - the control shown on the map 'Large', 'Small', or 'None'
 *  tcontrol - whether the type control is on the map or not: 'off' or 'on'
 *  scontrol - whether the scale control is on the map or not: 'off' or 'on'
 *  ocontrol - width and height of the overview control map: 'width,height'
webgeer's avatar
webgeer committed
 *  type - 'Map', 'Hybrid' or 'Satellite'
 *  drag - 'yes' or 'no' map is draggable.  Default is 'yes'
webgeer's avatar
webgeer committed
 *  markers - an array of associative arrays for the markers.
 *  shapes - an array of associative arrays for the overlays.
 *  track - a file containing a series of points in .plt format to be
 *  inserted into the node.
webgeer's avatar
webgeer committed
 *  feeds - an associative array of rss feeds
 *  wmss - an associative array of WMS services
 *
 *  Xmaps must be enabled for circle and polygon to work.
webgeer's avatar
webgeer committed
 *
 * @param $javascript
 * Some javascript to insert into function after drawing the map.
 * note that '{id}' will be replaced with the map id.
webgeer's avatar
webgeer committed
 *
 * @return
 * A string with the google map ready to be inserted into a node.
 *
 */
function gmap_draw_map($gmap, $javascript='') {
webgeer's avatar
webgeer committed
  global $base_url, $node;
    
    if (file_exists('misc/gxmarker.2.js')) {
      $gxmarkerjs=1;
    }
    else {
      $gxmarkerjs=0;
    }
webgeer's avatar
webgeer committed

    $clusterset=false;
    (!isset($gmap['width'])) ? $gmap['width']=GMAP_WIDTH: NULL;
    (!isset($gmap['height'])) ? $gmap['height']=GMAP_HEIGHT:NULL;
    (!isset($gmap['zoom'])) ? $gmap['zoom']=GMAP_ZOOM:NULL;
webgeer's avatar
webgeer committed
    (!isset($gmap['autozoom'])) ? $gmap['autozoom']=false:NULL;
webgeer's avatar
webgeer committed
    (!isset($gmap['align'])) ? $gmap['align']='':NULL;
    (!isset($gmap['id'])) ? $gmap['id']='map':NULL;
    (!isset($gmap['control'])) ? $gmap['control']=GMAP_CONTROL:NULL;
    (!isset($gmap['type'])) ? $gmap['type']=GMAP_TYPE:NULL;
    (!isset($gmap['tcontrol'])) ? $gmap['tcontrol']='off':NULL;
    (!isset($gmap['scontrol'])) ? $gmap['scontrol']='off':NULL;
//    (!isset($gmap['ocontrol'])) ? $gmap['ocontrol']='off':NULL;
    (!isset($gmap['drag'])) ? $gmap['drag']='yes':NULL;
webgeer's avatar
webgeer committed

webgeer's avatar
webgeer committed
    if (!isset($gmap['center'])&& isset($gmap['latlong'])){
      //backwards compatible with macros created by previous version of this program
      $gmap['center']=$gmap['latlong'];
    }
    elseif (!isset($gmap['center'])){
webgeer's avatar
webgeer committed
    }

    $style='width: '.gmap_todim($gmap['width']).'; height: '.gmap_todim($gmap['height']).';';
webgeer's avatar
webgeer committed

    switch (strtolower($gmap['align'])) {
      case 'left':
        $style .= ' float: left;';
        break;
      case 'right':
        $style .= ' float: right;';
        break;
      case 'center':
      case 'centre':
        $style .= ' margin-left: auto; margin-right: auto;';
webgeer's avatar
webgeer committed
//    $outtext .= "\n<!--\n\n".print_r($gmap)."\n\n -->\n";
webgeer's avatar
webgeer committed
    $outtext.='<div id="'.$gmap['id'].'" style="'.$style.'" class="gmap-map"></div>
webgeer's avatar
webgeer committed
              //<![CDATA[
              //initialize global gmap variables
webgeer's avatar
webgeer committed
              var  geocoder = null;
              var mycontrol=null;
              var mytypecontrol=null;
              var '.$gmap['id'].'=null;
              var gxmarkerjs='.$gxmarkerjs.';
              var gmapMarkerLocation="/'.variable_get('gmap_markerfiles',drupal_get_path('module', 'gmap') .'/markers').'";
              var markerlink='.variable_get('gmap_on_click',0).';
webgeer's avatar
webgeer committed
              var bounds = new GLatLngBounds();
              addLoadEvent(gmap_init);  
              addLoadEvent(gmap_load_'.$gmap['id'].');
           
webgeer's avatar
webgeer committed
              function gmap_load_'.$gmap['id'].'() {
webgeer's avatar
webgeer committed
                 '.$gmap['id'].' = new GMap2($("'.$gmap['id'].'"));
webgeer's avatar
webgeer committed
                   geocoder = new GClientGeocoder();
webgeer's avatar
webgeer committed
                 ';
                 

    if (isset($gmap['wmss'])) {
      $outtext.= '
                 // WMS layers ';
      $outtext.= '
                 '.$gmap['id'].'.getMapTypes().length = 0;';
      $ic=0;
      foreach ($gmap['wmss'] as $item) {
        $map_name = eregi_replace("[^a-z0-9_-]", "_", $item['name']);
        (!isset($item['minresolution'])) ? $item['minresolution']='1': NULL;
        (!isset($item['maxresolution'])) ? $item['maxresolution']='17': NULL;
        (!isset($item['format'])) ? $item['format']='image/gif': NULL;
        (!isset($item['merczoomlevel'])) ? $item['merczoomlevel']='5': NULL;

        $outtext.= '
                 var ccol_'.$map_name.' = new GCopyrightCollection("'.$item['name'].'");
        ';
        if(isset($item['copyrights'])){
          foreach ($item['copyrights'] as $copyright) {
            (!isset($copyright['bounds'])) ? $copyright['bounds']='-190,-90,180,90': NULL;
            (!isset($copyright['minzoom'])) ? $copyright['minzoom']='1': NULL;
            (!isset($copyright['text'])) ? $copyright['text']='': NULL;
            $coords = explode(",", $copyright['bounds']);
            $outtext.= '
                 var sw_'.$ic.' = new GLatLng('.$coords[0].','.$coords[1].',false);
                 var ne_'.$ic.' = new GLatLng('.$coords[2].','.$coords[3].',false);
                 var llb_'.$ic.' = new GLatLngBounds(sw_'.$ic.',ne_'.$ic.');
                 var cr_'.$ic.' = new GCopyright(1,llb_'.$ic.','.$copyright['minzoom'].',"'.$copyright['text'].'");
                 ccol_'.$map_name.'.addCopyright(cr_'.$ic.');
            ';
            $ic++;
          }
        }
        $outtext.= '
                 var t_'.$map_name.'= new GTileLayer(ccol_'.$map_name.','.$item['minresolution'].','.$item['maxresolution'].');
                 t_'.$map_name.'.myMercZoomLevel='.$item['merczoomlevel'].';
                 t_'.$map_name.'.myBaseURL=\''.$item['url'].'\';
                 t_'.$map_name.'.myLayers=\''.$item['layers'].'\';
                 t_'.$map_name.'.myFormat=\''.$item['format'].'\';
webgeer's avatar
webgeer committed
                 t_'.$map_name.'.myMapname=\''.$gmap['id'].'\';
                 t_'.$map_name.'.getTileUrl=CustomGetTileUrl;
                 ';
webgeer's avatar
webgeer committed
          if(isset($item['opacity'])){
            $outtext.= "t_".$map_name.".myOpacity=".$item['opacity'].";\n                 \n";
            $outtext.= "t_".$map_name.".getOpacity=customOpacity;\n                 \n";
          }
          if(isset($item['overlaywith'])){
            switch (strtolower($item['overlaywith'])) {
              case 'map':
                      $outtext.= "var l_".$map_name."1=[G_NORMAL_MAP.getTileLayers()[0]]; \n                 ";
                break;
              case 'hybrid':
                      //We overlay with roads only, not with the satellite imagery
                      $outtext.= "var l_".$map_name."1=[G_HYBRID_MAP.getTileLayers()[1]]; \n                 ";
                break;
              case 'satellite':
                      $outtext.= "var l_".$map_name."1=[G_SATELLITE_MAP.getTileLayers()[0]]; \n                 ";
                break;
              default:
                      $outtext.= "var l_".$map_name."1=[]; \n                 ";
            }
          }
        $outtext.= 'var l_'.$map_name.'2=[t_'.$map_name.'];';

            switch (strtolower($item['overlaywith'])) {
              case 'hybrid':
              $outtext.= '
                 var l_'.$map_name.'=l_'.$map_name.'2.concat(l_'.$map_name.'1);';
                break;
              default:
              $outtext.= '
                 var l_'.$map_name.'=l_'.$map_name.'1.concat(l_'.$map_name.'2);';
            }

        $outtext.= '                 
                 var m_'.$map_name.' = new GMapType(l_'.$map_name.', G_SATELLITE_MAP.getProjection(), "'.$item['name'].'", G_SATELLITE_MAP); 
                 '.$gmap['id'].'.addMapType(m_'.$map_name.');
                 ';
      }
      $outtext.= '
                 '.$gmap['id'].'.addMapType(G_NORMAL_MAP);
                 '.$gmap['id'].'.addMapType(G_SATELLITE_MAP);
                 '.$gmap['id'].'.addMapType(G_HYBRID_MAP);
                 ';
    }

    $outtext.= $gmap['id'].'.setCenter(new GLatLng('.$gmap['center'].'), '.$gmap['zoom'].');
                 ';

webgeer's avatar
webgeer committed
    switch (strtolower($gmap['control'])) {
      case 'small':
        $outtext .='mycontrol=new GSmallMapControl();
webgeer's avatar
webgeer committed
                '.$gmap['id'].'.addControl(mycontrol);
                 ';
        break;
      case 'large':
      $outtext .='mycontrol=new GLargeMapControl();
webgeer's avatar
webgeer committed
              '.$gmap['id'].'.addControl(mycontrol);
              ';
    }
webgeer's avatar
webgeer committed

    if (strtolower($gmap['tcontrol'])=='on' || strtolower($gmap['tcontrol'])=='yes') {
      $outtext .= $gmap['id'].'.addControl(mytypecontrol=new GMapTypeControl());
                ';
    }
    if (strtolower($gmap['scontrol'])=='on' || strtolower($gmap['scontrol'])=='yes') {
      $outtext .= $gmap['id'].'.addControl(myscalecontrol=new GScaleControl());
                ';
    }
    if (isset($gmap['ocontrol']) && strlen($gmap['ocontrol']) > 0) {
      $osize = explode(",", $gmap['ocontrol']);
      if (intval($osize[0]) > 0 and intval($osize[1]) > 0) {
        $outtext .= $gmap['id'].'.addControl(myovcontrol=new GOverviewMapControl(new GSize('.$osize[0].','.$osize[1].')));
                ';
      }
    }
webgeer's avatar
webgeer committed
    switch (strtolower($gmap['type'])) {
      case 'hybrid':
webgeer's avatar
webgeer committed
        $outtext .= $gmap['id'].".setMapType(G_HYBRID_MAP); \n";
webgeer's avatar
webgeer committed
        break;
webgeer's avatar
webgeer committed
        $outtext .= $gmap['id'].".setMapType(G_SATELLITE_MAP); \n";
webgeer's avatar
webgeer committed
    }
    if (isset($gmap['drag']) && strtolower($gmap['drag'])=='no') {
webgeer's avatar
webgeer committed
      $outtext .= $gmap['id'].".disableDragging(); \n";
webgeer's avatar
webgeer committed
    else {
      $outtext .= "keyboardhandler=new GKeyboardHandler(".$gmap['id'].");\n";
webgeer's avatar
webgeer committed
    }
webgeer's avatar
webgeer committed

webgeer's avatar
webgeer committed
    if (isset($gmap['tracks'])) {
      foreach ($gmap['tracks'] as $value) {
        $tt['points']=array();
        if($trackFH = fopen($tt['filename'], "r")) {
          while(!feof($trackFH)) {
            $line = fgets($trackFH, 4096);
            $line_exploded = explode(",", $line);
            if(count($line_exploded) == 7) {
webgeer's avatar
webgeer committed
              $tt['points'][]= trim($line_exploded[0]).','.trim($line_exploded[1]);
webgeer's avatar
webgeer committed
      }
    }
      if (GMAP_CLUSTER) {
        $outtext .="\n var clusterer = new Clusterer(".$gmap['id'].");\n";
webgeer's avatar
webgeer committed
        $clusterset=true;
      $lastmarker='';
      foreach ($gmap['markers'] as $item) {
        $mymarker='';
        $mytext='';
        if (isset($item['markername'])){
          if ($item['markername']==$lastmarker) {
            $i++;
          }
          else {
            $lastmarker=$item['markername'];
            $i=1;
          }
webgeer's avatar
webgeer committed
          if (file_exists(variable_get('gmap_markerfiles',drupal_get_path('module', 'gmap') .'/markers').'/'.$item['markername'].$i.'.png')) {
            $mymarker=$base_url.'/'.variable_get('gmap_markerfiles',drupal_get_path('module', 'gmap') .'/markers').'/'.$item['markername'].$i.'.png';
webgeer's avatar
webgeer committed
          elseif (file_exists(variable_get('gmap_markerfiles',drupal_get_path('module', 'gmap') .'/markers').'/'.$item['markername'].'.png')) {
            $mymarker=$base_url.'/'.variable_get('gmap_markerfiles',drupal_get_path('module', 'gmap') .'/markers').'/'.$item['markername'].'.png';
        if (isset($item['text']) && strlen(trim($item['text']))>0) {
          $mytext=$item['text'];
        }
        elseif (isset($item['label']) && strlen(trim($item['label']))>0) {
webgeer's avatar
webgeer committed
        }
        $divstyle='';
        if (isset($item['winwidth']) && trim($item['winwidth']) != '') {
          $divstyle='width: '.$item['winwidth'].'; ';
        }
        if (strlen($divstyle) >0 && strlen(trim($mytext))>0) {
          $mytext = '<div class="gmap-popup" style="'.$divstyle.'">'.$mytext.'</div>';
        }
webgeer's avatar
webgeer committed
          //special line to insert nodeid into text. Uncomment this for it to work.
          $mytext=str_replace('##NODE##', $node->nid, $mytext);
          $mytext = '<div class="gmap-popup">'.$mytext.'</div>';
        }
        if (isset($item['tooltip'])) {
          $tooltip=check_plain($item['tooltip']);
webgeer's avatar
webgeer committed
        if (isset($item['link'])) {
          $link=$item['link'];
        }
        else {
          $link='';
        }
        if (GMAP_CLUSTER) {
webgeer's avatar
webgeer committed
          $outtext .='clusterer.AddMarker(createGMarker(new GLatLng('.$item['point'].'),\''.$mytext.'\',\''.$mymarker."','$tooltip','$link'));\n";
webgeer's avatar
webgeer committed
          $clusterset=true;
        }
        else {
webgeer's avatar
webgeer committed
          $outtext .=$gmap['id'].'.addOverlay(createGMarker(new GLatLng('.$item['point'].'),\''.$mytext.'\',\''.$mymarker."','$tooltip','$link'));\n";
webgeer's avatar
webgeer committed
          if($gmap['autozoom']){
            $outtext .='bounds.extend(new GLatLng('.$item['point'].'));'."\n";
          }
webgeer's avatar
webgeer committed
      }
    }
webgeer's avatar
webgeer committed
    if ($gmap['autozoom']){
      $outtext .= 'var center_lat = (bounds.getNorthEast().lat() + bounds.getSouthWest().lat()) / 2.0;
                   var center_lng = (bounds.getNorthEast().lng() + bounds.getSouthWest().lng()) / 2.0;
                   var center = new GLatLng(center_lat,center_lng);
                   var zoom = '.$gmap['id'].'.getBoundsZoomLevel(bounds);
                   '.$gmap['id'].'.setCenter(center,zoom);';
    }
    if (isset($gmap['shape']) && !variable_get('gmap_xmaps',0)) {
webgeer's avatar
webgeer committed
      //if xmaps is not enabled then just show the lines using straight google maps.
      foreach ($gmap['shape'] as $value) {
        $style ='';
        if (trim($value['type'])=='line') {
          if (isset($value['color'])) {
            $style .=",'".$value['color']."'";
            if (isset($value['width'])) {
              $style .=','.$value['width'];
              if (isset($value['opacity'])) {
                $style .=','.$value['opacity'];
              }
            }
          }
webgeer's avatar
webgeer committed
          $linetxt ='new GLatLng('.implode('), new GLatLng(',$value['points']).')';
//          foreach ($value['points'] as $lvalue) {
//            if (strlen($linetxt)>0) {
//              $linetxt .= ', ';
//            }
//            $linetxt .="new GPoint(".$lvalue.")";
//          }
          $outtext .="\n".$gmap['id'].".addOverlay(new GPolyline([$linetxt] $style));";
    elseif (isset($gmap['shape'])) {
      foreach ($gmap['shape'] as $value) {
        $linestyle=array();
        $fillstyle=array();
          $linestyle[] = 'color: "'.$value['color'].'" ';
          $linestyle[] = 'weight: '.$value['width'];
          $linestyle[] = 'opacity: '.$value['opacity'];
          $linestyle[] = 'pattern: ['.$value['pattern'].'] ';
          $linestyle[] = 'text: "'.$value['text'].'" ';
          $fillstyle[] = 'color: "'.$value['fillcolor'].'" ';
        }
        if (isset($value['fillopacity'])){
          $fillstyle[] = 'opacity: '.$value['fillopacity'];
        $outtext .= 'var lineStyle = {'.implode(',',$linestyle)."};\n";
        if (count($fillstyle)>0) {
          $outtext .= 'var fillStyle = {'.implode(',',$fillstyle)."};\n";
webgeer's avatar
webgeer committed
        }
        else {
          $outtext .= "var fillStyle = null;\n";
        }

        switch ($value['type']) {
          case 'line':
            $outtext .= "var points = []; \n";

            foreach ($value['points'] as $lvalue) {
              $outtext .="points.push(new GPoint(".$lvalue."));\n";
            }
            $outtext .= $gmap['id'].".addOverlay(new XPolyline(points, lineStyle));\n";
            break;
          case 'circle':
            //create a circle by using a radius in km
            if (empty($value['sides'])){
              $value['sides']=36;
            }
            $outtext .= 'var radius = new XDistance('.$value['radius'].",XDistance.KM);\n";
            $outtext .= 'var centre = new GPoint('.$value['center'].");\n";
            $outtext .= $gmap['id'].".addOverlay(XPolygon.createRegularPolygonFromRadius(centre, radius, ".$value['sides'].", 0, lineStyle, fillStyle));\n";
            break;
          case 'rpolygon':
            if (isset($value['sides'])){
              $value['sides']=36;
            }
            $outtext .= "var centre = new GPoint(".$value['center'].");\n";
            $outtext .= $gmap['id'].".addOverlay(XPolygon.createRegularPolygonFromPoint( new GPoint(".$value['center']."), new GPoint(".$value['point']."), 36, lineStyle, fillStyle));\n";
            break;
          case 'polygon':
            $outtext .= "var points=[];\n";
            foreach ($value['points'] as $pvalue) {
              $outtext .= "points.push(new GPoint($pvalue));\n";
            }
            $outtext .= $gmap['id'].".addOverlay(new XPolygon(points, lineStyle, fillStyle));\n";
webgeer's avatar
webgeer committed
    if (isset($gmap['feeds'])) {
//      if (GMAP_CLUSTER && !$clusterset) {
//        $outtext .="\n var clusterer = new Clusterer(".$gmap['id'].");\n";
 //     }
      foreach ($gmap['feeds'] as $item) {
        $mymarker='';
        if (isset($item['markername'])){
webgeer's avatar
webgeer committed
          if (file_exists(variable_get('gmap_markerfiles',drupal_get_path('module', 'gmap') .'/markers').'/'.$item['markername'].'.png')) {
            $mymarker=$base_url.'/'.variable_get('gmap_markerfiles',drupal_get_path('module', 'gmap') .'/markers').'/'.$item['markername'].'.png';
webgeer's avatar
webgeer committed
          }
        }
        if (isset($item['url'])) {
          $outtext.= "parseGeoRSS(".$gmap['id'].",'".$item['url']."','$mymarker');\n";
        }
      }
    }
    if (strlen($javascript)>0) {
      $javascript=str_replace('{id}',$gmap['id'],$javascript);
      $outtext .=$javascript;
    }
    $outtext .="\n } \n   //]]>
               </script>";
webgeer's avatar
webgeer committed
    return $outtext;
}

/**
 *
 * Cleans the gmap variables to prevent javascript interjection
 *
 * @param $gmap
 * A Gmap variable
 *
 * @return
 * A GMap variable with any dangerous text removed.
webgeer's avatar
webgeer committed
 *
 * This does not really do much of anything right now.
webgeer's avatar
webgeer committed
 */
webgeer's avatar
webgeer committed
  //sanitizes the gmap variables to reduce the possibility of javascript inserts
  foreach ($gmap as $key=>$value) {
    if ($key=='id') {
      $out = array();
      preg_match('([a-zA-Z1-9_-]*)', $value, $out);
      if (strlen($out[0])==0)  {
        $out[0]='map';
      }
      $gmap[$key]=$out[0];
webgeer's avatar
webgeer committed
    }
    else {
      $gmap[$key]=str_replace(';','',$value);
webgeer's avatar
webgeer committed
    }
webgeer's avatar
webgeer committed
 *
 * @param $instring
 * A string with the settings of gmap insertion in the format var=setting|var2=setting2
 *  The possible variables are
 *  id - the id of the map every map on a page must have a unique id
 *  width - width of the map
 *  height - height of the map
webgeer's avatar
webgeer committed
 *  center - a string of the longitude and latitude of the centre of the map
webgeer's avatar
webgeer committed
 *  zoom - the zoom factor of the google map
 *  align - the alignment of the map 'right', 'left' or 'center'
 *  control - the control shown on the map 'Large', 'Small', or 'None'
 *  type - 'Map', 'Hybrid' or 'Satellite'
 *  points/markers - a string of points to mark on the map with + between
 *          each point
 *  line - the line is defined by a set of points separated by a +
 *  track - Draws a line based on the points in the .plt file
 *  The following shape types require XMaps:
 *  circle - a circle based on a center point and a radius in km separated
 *          by a + and optionally can include the number of sizes.
 *  rpolygon - a regular polygon is defined by the center point and a point
 *          on the permiter separated by a +
 *  polygon - a polygon is defined by a set of points
 *
 *  Each of the shapes types can optionally have charecteristics of colour,
 *  width, opacity, pattern, text, fill colour, fill opacity.  Pattern, text
 *  and fill are all only used by xmaps.
 *      color - hexadecimal for the colour include the '#'
webgeer's avatar
webgeer committed
 *
webgeer's avatar
webgeer committed
 * @return
 * A string with the google map ready to be inserted into a node.
 *
 */
webgeer's avatar
webgeer committed
function gmap_parse_macro($instring,$ver=2) {
webgeer's avatar
webgeer committed

  if (substr(trim($instring),-1)==']') {
    $instring=substr(trim($instring), 0, -1);
webgeer's avatar
webgeer committed
  }
  if (substr($instring, 0,5)=='[gmap') {
    $instring=substr($instring, 6);
  }
webgeer's avatar
webgeer committed
  $statements=explode('|', $instring);
  $j=0;
webgeer's avatar
webgeer committed
  while (isset($statements[$j])) {
webgeer's avatar
webgeer committed
    $t=explode('=', $statements[$j],2);
webgeer's avatar
webgeer committed
      case 'center':
        $gmap['center']=gmap_dopoint(trim($t[1]),$ver);
        break;
      case 'zoom':
        if ($ver==1) {
          $gmap['zoom'] = 18-trim($t[1]);
          if ($gmap['zoom']<1) {
             $gmap['zoom']=1;
          }
        }
        else {
          $gmap['zoom'] = trim($t[1]);
        }
        break;
      case 'points':
      case 'markers':
        unset($markername);
        if (strpos($t[1],'::')) { // note: we don't care about case starting with ':'
          list($markername,$t[1])=explode('::',$t[1],2);
        }
        unset($ttt);
        $ttt = explode('+',$t[1]);
        for ($i =0; $i<count($ttt); $i++) {
          unset($tt);
          if (strpos($ttt[$i],':')) {
            list($tt['point'],$tt['label']) = explode(':',$ttt[$i],2);
webgeer's avatar
webgeer committed
            $tt['point']=gmap_dopoint($tt['point'],$ver);
webgeer's avatar
webgeer committed
            $tt['point']=gmap_dopoint($ttt[$i],$ver);
          }
          if (isset($markername)){
            $tt['markername']=$markername;
          }
          $gmap['markers'][]=$tt;
        }
        break;

      case 'track':
        if (strpos($t[1],':')) { // note: we don't care about case starting with ':'
          list($configs,$t[1])=explode(':',$t[1],2);
          list($tt['color'],$tt['width'],$tt['opacity'],$tt['pattern'],$tt['text'])=explode('/',$configs);
        }
        $tt['filename'] =$t[1];
webgeer's avatar
webgeer committed
        $gmap['tracks'][] = $tt;
webgeer's avatar
webgeer committed
        
      case 'feed':
        if (strpos($t[1],'::')) { // note: we don't care about case starting with ':'
          list($markername,$t[1])=explode('::',$t[1],2);
        }
webgeer's avatar
webgeer committed
        $tt['url'] =$t[1];
        if (isset($markername)){
          $tt['markername']=$markername;
        }
webgeer's avatar
webgeer committed
        $gmap['feeds'][] = $tt;
        break;
      case 'wmss':
        unset($ttt);
        $ttt = explode('+',$t[1]);
        for ($i =0; $i<count($ttt); $i++) {
          unset($tt);
webgeer's avatar
webgeer committed
          list($tt['name'],$tt['url'],$tt['format'],$tt['layers'],$tt['minresolution'],$tt['maxresolution'],$tt['copyrights'],$tt['overlaywith'],$tt['merczoomlevel'],$tt['opacity'])=explode('::',$ttt[$i]);
          $crr = explode('/',$tt['copyrights']);
          unset($tt['copyrights']);
          for ($k =0; $k<count($crr); $k++) {
            unset($cr);
            list($cr['minzoom'],$cr['bounds'],$cr['text'])=explode(':',$crr[$k]);
            $tt['copyrights'][]=$cr;
          }
          $gmap['wmss'][]=$tt;
        }
        break;

      case 'line2':
        if (empty($tt['color'])){
        }
      case 'line3':
        if (empty($tt['color'])){
        }
      case 'line':
        if (strpos($t[1],':')) { // note: we don't care about case starting with ':'
          list($configs,$t[1])=explode(':',$t[1],2);
          list($tt['color'],$tt['width'],$tt['opacity'],$tt['pattern'],$tt['text'])=explode('/',$configs);
        }
        $tt['points'] = explode('+',$t[1]);
webgeer's avatar
webgeer committed
        if ($ver==1) {
          foreach ($tt['points'] as $key=>$value) {
            $tt['points'][$key]=gmap_dopoint($value,$ver);
          }
        }
        $tt['type'] = 'line';
        $gmap['shape'][] = $tt;
        break;

      case 'rpolygon':
        if (strpos($t[1],':')) { // note: we don't care about case starting with ':'
          list($configs,$t[1])=explode(':',$t[1],2);
          list($tt['color'],$tt['width'],$tt['opacity'],$tt['pattern'],$tt['text'],$tt['fillcolor'],$tt['fillopacity'])=explode('/',$configs);
        }
        list($tt['center'],$tt['point'],$tt['sides']) = explode('+',$t[1]);
        $tt['type']='rpolygon';
        $gmap['shape'][] = $tt;
        break;

      case 'circle':
        if (strpos($t[1],':')) { // note: we don't care about case starting with ':'
          list($configs,$t[1])=explode(':',$t[1],2);
          list($tt['color'],$tt['width'],$tt['opacity'],$tt['pattern'],$tt['text'],$tt['fillcolor'],$tt['fillopacity'])=explode('/',$configs);
        }
        list($tt['center'],$tt['radius'],$tt['sides'])= explode('+',$t[1]);
        $tt['type']='circle';
        $gmap['shape'][] = $tt;
        break;

      case 'polygon':
        if (strpos(':',$t[1])) { // note: we don't care about case starting with ':'
          list($configs,$t[1])=explode(':',$t[1],2);
          list($tt['color'],$tt['width'],$tt['opacity'],$tt['pattern'],$tt['text'],$tt['fillcolor'],$tt['fillopacity'])=explode('/',$configs);
        }
        $tt['points'] = explode('+',$t[1]);
        $tt['type']='polygon';
        $gmap['shape'][] = $tt;
        break;


      default:
        $gmap[trim($t[0])] = trim($t[1]);
      }
webgeer's avatar
webgeer committed
    $j++;
  }
/**
 *
 * Creates a map that can be interactively used to fill a form with a
 * location (latitude, longitude and zoom level)
 *
webgeer's avatar
webgeer committed
 * @param $map
 * Either a macro to use as the base map for setting a location, or an already set map associative array.
 * @param $form
 * A formset associative array.  Cannot be more than one deep.
 * @param $fields
 * An associative array for the field names.  'latitude', 'longitude'=>name of respective array, 'address' is optional.
 * @return
 * A string with the google map code to be inserted onto the page.
 *
 */
webgeer's avatar
webgeer committed
function gmap_set_location($map,&$form,$fields) {

  if (!is_array($map)){
    $map=gmap_parse_macro($map);
  }
webgeer's avatar
webgeer committed
  $zoom=$map['zoom'];

webgeer's avatar
webgeer committed
  $form[$fields['latitude']]['#id']='gmap-latitude';
  $form[$fields['latitude']]['#attributes']=array('onchange'=>'gmap_textchange('.$map['id'].');');
  $form[$fields['longitude']]['#id']='gmap-longitude';
  $form[$fields['longitude']]['#attributes']= array('onchange'=>'gmap_textchange('.$map['id'].');');
  if (isset($fields['address'])) {
    $form[$fields['address']]['#id']='gmap-address';
    $form[$fields['address']]['#attributes']= array('onchange'=>'gmap_geocodeaddress('.$map['id'].', this.value);');
  }
webgeer's avatar
webgeer committed

webgeer's avatar
webgeer committed
  if ($lonform['#default_value']!=0 || $latform['#default_value']!=0) {
webgeer's avatar
webgeer committed
    $myjava .= '{id}.panTo(new GLatLng('.$latform['#default_value'].', '.$lonform['#default_value'].'));'."\n".
             '{id}.addOverlay(thispoint=new GMarker(new GLatLng('.$latform['#default_value'].', '.$lonform['#default_value'].')));'."\n";
webgeer's avatar
webgeer committed
  $myjava .=  '   GEvent.addListener({id}, "click", function(overlay, point) {
                  {id}.checkResize();
webgeer's avatar
webgeer committed
                    {id}.zoomIn();
                    {id}.zoomIn();
                    if (thispoint) {
                      {id}.removeOverlay(thispoint);
                    }
                    {id}.addOverlay(thispoint=new GMarker(point));
webgeer's avatar
webgeer committed
                    $("gmap-latitude").value=point.lat();
                    $("gmap-longitude").value=point.lng();
webgeer's avatar
webgeer committed
                    {id}.panTo(point);
                  else {
                    {id}.removeOverlay(thispoint);
                    $("gmap-latitude").value="";
                    $("gmap-longitude").value="";
                    thispoint=null;
                  }
webgeer's avatar
webgeer committed
               // GEvent.addListener({id}, "dragend", function() {{id}.checkResize();} )
webgeer's avatar
webgeer committed
  $out = gmap_draw_map($map,$myjava);
  

  return $out;
}

function _gmap_doheader(){
  static $gmap_initialized = FALSE;
  if (variable_get('gmap_method','Static')=='None' || $gmap_initialized) {
webgeer's avatar
webgeer committed

  theme_add_style(drupal_get_path('module','gmap'). '/gmap.css');
  drupal_add_js('misc/drupal.js');
  drupal_add_js(drupal_get_path('module','gmap'). '/gmap.js');
webgeer's avatar
webgeer committed
  $js = '<script src="http://maps.google.com/maps?file=api&amp;v='.GMAP_API_V.'&amp;key='.variable_get('googlemap_api_key', '').'" type="text/javascript"></script>';
webgeer's avatar
webgeer committed
  drupal_set_html_head($js);
webgeer's avatar
webgeer committed
  if (GMAP_XMAPS) {
webgeer's avatar
webgeer committed
    drupal_add_js(variable_get('gmap_xmaps_script','misc/xmaps.1c.js'));
  }
  if (GMAP_CLUSTER){
    if (file_exists('misc/Clusterer2.js')) {
      drupal_add_js('misc/Clusterer2.js');
    }
    else {
      drupal_add_js('http://www.acme.com/javascript/Clusterer2.js');
    }
  }
  if (GMAP_WMS){
    if (file_exists('misc/wms-gs.js')) {
      drupal_add_js('misc/wms-gs.js');
    } elseif (file_exists('misc/wms236.js')) {
      drupal_add_js('misc/wms236.js');
webgeer's avatar
webgeer committed
    } elseif (file_exists('misc/wms-gs-1_0_1.js')) {
      drupal_add_js('misc/wms-gs-1_0_1.js');
    } elseif (file_exists('misc/wms-gs-1_0_0.js')) {
      drupal_add_js('misc/wms-gs-1_0_0.js');
    } else {
webgeer's avatar
webgeer committed
      drupal_add_js('http://dist.codehaus.org/geoserver/gmaps-geoserver_scripts/wms-gs-1_0_1.js');
webgeer's avatar
webgeer committed
  }
webgeer's avatar
webgeer committed
function _gmap_prepare($intext) {
webgeer's avatar
webgeer committed
  $mapexp = '/\[gmap([^\[\]]+ )* \] /x';
webgeer's avatar
webgeer committed
  preg_match_all($mapexp, $intext, $matches);
webgeer's avatar
webgeer committed

  while (isset($matches[1][$i])) {
    $out[0][$i] = $matches[0][$i];
webgeer's avatar
webgeer committed
    if ($matches[1][$i][0]=='1'){
      $ver=1;
      $matches[1][$i]=substr($matches[0][$i],1);
    }
    else {
      $ver=2;
    }
    $out[1][$i] = gmap_draw_map(gmap_parse_macro($matches[1][$i],$ver));
webgeer's avatar
webgeer committed
    $i++;
  } // endwhile process macro
  return $out;
}

function gmap_todim($instring) {
  if (strpos($instring,'px')) {
    return intval($instring).'px';
  }
  elseif (strpos($instring,'%')) {
    return intval($instring).'%';
  }
  else {
    return intval($instring).'px';
  }
}

webgeer's avatar
webgeer committed
function gmap_dopoint($point,$ver) {
  if ($ver==1) {
    $lonlat=explode(',',$point,2);
    return $lonlat[1].','.$lonlat[0];
  }
  else {
    return $point;
  }
}

webgeer's avatar
webgeer committed
function gmap_filter($op, $delta = 0, $format = -1, $text = '') {
  switch ($op) {
    case 'list':
      return (array(0 => t('GMap filter')));

    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.');
webgeer's avatar
webgeer committed
    case 'process':
      $gmaps=_gmap_prepare($text);   //returns an array of $tables[0] = table macro $table[1]= table html
      if ($gmaps) {                    // there are table macros in this node
        return str_replace($gmaps[0], $gmaps[1], $text);
      }
      else {
        return $text;
      }

    case 'prepare':
      return $text;

    
      return (variable_get('gmap_method', 'Static')=='Dynamic');
webgeer's avatar
webgeer committed
  }
}

function gmap_filter_tips($delta, $format, $long = false) {

  if(user_access('create macro')) { // only display macro if user can create one
webgeer's avatar
webgeer committed
    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.');
  }
webgeer's avatar
webgeer committed
}

function gmap_menu($may_cache) {
  $items=array();

  if ($may_cache) {
      'path' => 'map',
      'type' => MENU_ITEM_GROUPING,
      'title' => t('google maps'),
      'access' => user_access('create macro')||user_access('show user map')||user_access('show node map'),
    $items[] = array(
      'path' => 'map/macro',
      'type' => MENU_NORMAL_ITEM,
webgeer's avatar
webgeer committed
      'title' => t('build a GMap macro'),
      'access' => user_access('create macro'),
      'callback' => 'gmap_macro_page',
    );
webgeer's avatar
webgeer committed
  }
webgeer's avatar
webgeer committed
  if (!$may_cache && (strlen(variable_get('googlemap_api_key', '')) > 0)) {
    if (variable_get('gmap_method', 'Static')=='Static') {
      _gmap_doheader();
    }
webgeer's avatar
webgeer committed
  }
  return $items;
}

function gmap_settings() {
  //note the same google api key variable name as in the googlemap module is used
webgeer's avatar
webgeer committed
  //note the name of the variable for center of the map is latlong although the format is actually longitude, latitude
  $form['initialization']=array(
    '#type' => 'fieldset',
    '#title' => t('Google Map Initialize'),
  );
  $form['initialization']['googlemap_api_key']=array(
    '#type' => 'textfield', 
    '#title' => t('Google map API key'),
    '#default_value'=>variable_get('googlemap_api_key', ''), 
    '#size' => 50, 
    '#maxlength' => 255,
    '#description' => t('Your personal Googlemaps API key.  You must get this for each separate website at <a href="http://www.google.com/apis/maps/">Google Map API website</a>.'),
  );
  $form['initialization']['gmap_method']=array(
    '#type' => 'select',
    '#title' => t('Google map method'),
    '#options'=> array('Static'=>t('Static'), 'Dynamic'=>t('Dynamic'), 'None'=>t('None')),
    '#default_value'=>GMAP_LOAD_METHOD,
    '#description' => t('How is the Google Map initialization script run:<br /><strong>Static</strong> - Loaded on every page, <br /><strong>Dynamic</strong> - Initialization script runs only on pages with google maps, but all pages with the gmap filter will not be cached. <br /><strong>None</strong> - Google map initialization script must be loaded some other way.'),
  );
  // xmaps not compatible with v2 of the Google Map API
  /*
  $form['initialization']['gmap_xmaps']=array(
    '#type'=>'checkbox', 
    '#title'=>t('Enable Xmaps features (circles and polygons)'), 
    '#default_value'=>GMAP_XMAPS,
    '#description'=>t('Enables XMaps features. XMaps is currently a <em>beta</em> release available from <a href="http://xmaps.busmonster.com/">xmaps.busmonser.com</a>. '),
  );
  $form['initialization']['gmap_xmaps_script']=array(
    '#type' => 'textfield', 
    '#title' => t('Location of XMaps script'), 
    '#default_value'=>GMAP_XMAPS_SCRIPT,
    '#size' => 25,
    '#maxlength' => 50,
  );
  */
  $form['initialization']['gmap_cluster']=array(
    '#type'=>'checkbox', 
    '#title'=>t('Enable clusterer.js'),
    '#default_value'=>GMAP_CLUSTER,
    '#description'=>t('Enables Jef Poskanzer\'s excellent <a href="http://www.acme.com/javascript/#Clusterer">clusterer.js</a> script. (useful for a large number of markers on a single map)'),
  );
  $form['initialization']['gmap_on_click']=array(
    '#type'=>'radios',
                                                 '#title'=>t('On Click Marker'),
                                                 '#default_value'=>variable_get('gmap_on_click',0),
                                                 '#options'=>array(t('Open info window'), t('Open link')),
    '#description'=>t('For markers that have link associated with them, it can either an info window or go directly to the link.'),
  );
  $form['defaults']=array(
    '#type' => 'fieldset',
    '#title' => t('Default map settings'),
  );
  $form['defaults']['gmap_default_width']=array(
    '#type'=>'textfield',
    '#title'=>t('Default width'),
    '#default_value'=>GMAP_WIDTH,