Changeset 6

Show
Ignore:
Timestamp:
10/09/08 09:44:59 (3 years ago)
Author:
blundeln
Message:

The user can now manually set the node location by clicking on map

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • drupal/modules/simple_maps/trunk/TODO

    r5 r6  
    1 - edit node should display dynamic map 
    2   - when moved, will override geocoded location of node. 
    3   - when blank will use address geocode. 
     11.0 
     2- help page. 
     31.1 
    44- view node should display static map 
  • drupal/modules/simple_maps/trunk/simple_maps.module

    r5 r6  
    7070  if ($op == "submit" or $op == "insert") { 
    7171    if ($node->nid) { 
    72       _set_node_geocode($node); 
     72      // If box ticked, use fields to set this 
     73      // else, re-geocode. 
     74      if ($node->loc_manual_select and $node->loc_location_latitude and $node->loc_location_longitude) { 
     75        $loc = _get_node_location($node->nid); 
     76        $loc["latitude"] = $node->loc_location_latitude; 
     77        $loc["longitude"] = $node->loc_location_longitude; 
     78        $loc["manual"] = True; 
     79        _set_node_location($node->nid, $loc); 
     80        drupal_set_message("The geographic location of '$node->title' has been set manually."); 
     81        //d("node",$node); 
     82      } else { 
     83        _geocode_node($node); 
     84        $loc = _get_node_location($node->nid); 
     85        $loc["manual"] = False; 
     86        _set_node_location($node->nid, $loc); 
     87      } 
    7388    } 
    7489  } 
     
    7691} 
    7792 
     93 
     94/* Hook our location fields into node edit form. */ 
    7895function simple_maps_form_alter($form_id, &$form) {  
    7996   
     
    106123 
    107124 
    108 function _location_form($loc) { 
    109   $form = array(); 
    110  
    111   $form['coordinates'] = array( 
    112     '#type' => 'fieldset', 
    113     '#title' => t('Location'), 
    114     '#weight' => 5, 
    115     '#collapsible' => $type!='user', 
    116     '#collapsed' => FALSE, 
    117   ); 
    118  
    119   // Reserve spot for map. 
    120   $form['coordinates']['gmap_node'] = array(); 
    121  
    122   $form['coordinates']['gmap_location_latitude'] = array( 
    123     '#type' => 'textfield', 
    124     '#title' => t('Latitude'), 
    125     '#default_value' => $loc['latitude'], 
    126     '#size' => 30, 
    127     '#maxlength' => 120, 
    128   ); 
    129  
    130   $form['coordinates']['gmap_location_longitude'] = array( 
    131     '#type' => 'textfield', 
    132     '#title' => t('Longitude'), 
    133     '#default_value' => $loc['longitude'], 
    134     '#size' => 30, 
    135     '#maxlength' => 120, 
    136     '#description' => t('The latitude and longitude will be entered here when you double-click on a location in the interactive map above. You can also fill in the values manually.'), 
    137   ); 
    138  
    139   $map = gmap_defaults(); 
    140   $map["width"] = "300px"; 
    141   $map["height"] = "300px"; 
    142  
    143   $form['coordinates']['gmap_node']['#value'] = gmap_set_location($map, $form['coordinates'], array('latitude' => 'gmap_location_latitude', 'longitude' => 'gmap_location_longitude')); 
    144  
    145   return $form; 
    146 } 
    147125 
    148126/* 
     
    207185/* 
    208186 * =============================== 
     187 * FORMS 
     188 * =============================== 
     189 */ 
     190 
     191function _location_form($loc) { 
     192  $form = array(); 
     193 
     194  $form['coordinates'] = array( 
     195    '#type' => 'fieldset', 
     196    '#title' => t('Geographic Location'), 
     197    '#weight' => 5, 
     198    '#collapsible' => $type!='user', 
     199    '#collapsed' => FALSE, 
     200  ); 
     201 
     202  // Reserve spot for map. 
     203  $form['coordinates']['loc_manual_select'] = array( 
     204    '#type' => 'checkbox', 
     205    '#title' => t('Use manually selected location: Tick this box and click on the map to correct the location if it was not picked up automatically from the address.'), 
     206    '#default_value' => $loc['manual'], 
     207  ); 
     208   
     209  $form['coordinates']['loc_node'] = array(); 
     210 
     211  $form['coordinates']['loc_location_latitude'] = array( 
     212    '#type' => 'hidden', 
     213    '#title' => t('Latitude'), 
     214    '#default_value' => $loc['latitude'], 
     215    '#size' => 30, 
     216    '#maxlength' => 120, 
     217  ); 
     218 
     219  $form['coordinates']['loc_location_longitude'] = array( 
     220    '#type' => 'hidden', 
     221    '#title' => t('Longitude'), 
     222    '#default_value' => $loc['longitude'], 
     223    '#size' => 30, 
     224    '#maxlength' => 120, 
     225  ); 
     226 
     227  $map = gmap_defaults(); 
     228  $map["width"] = "300px"; 
     229  $map["height"] = "300px"; 
     230 
     231  $form['coordinates']['loc_node']['#value'] = gmap_set_location($map, $form['coordinates'], array('latitude' => 'loc_location_latitude', 'longitude' => 'loc_location_longitude')); 
     232 
     233  return $form; 
     234} 
     235 
     236/* 
     237 * =============================== 
    209238 * MAIN FUNCTIONS 
    210239 * =============================== 
     
    213242/* Looks at permutations of the address and postcode to try to improve accuracy of google api. */ 
    214243function _smart_geocode($address, $areaCode=Null) { 
    215  
    216   //XXX 
    217   $areaCode = Null; 
    218244 
    219245  // Get area code (eg. postcode). 
     
    290316 
    291317/* This automatically tries to find a location for a node, based on a CCK field called 'address' or on a 'postcode' field. */ 
    292 function _set_node_geocode(&$node) { 
     318function _geocode_node(&$node) { 
    293319   
    294320  $addressField = variable_get("simplemap.addressfield", "field_address"); 
     
    315341    
    316342  /* Store the location info for this node. */ 
    317   variable_set("simplemap.nloc-$node->nid", $location); 
     343  _set_node_location($node->nid, $location); 
    318344  drupal_set_message("The geographic location of '$node->title' has been found and set."); 
     345} 
     346 
     347 
     348/* Sets the location of this node. */ 
     349function _set_node_location($nid, $loc) { 
     350  // Gets the geographic location of a node with nid. 
     351  return variable_set("simplemap.nloc-$nid", $loc); 
    319352} 
    320353 
     
    427460  while ($item = db_fetch_object($results)) { 
    428461    $node = node_load($item->nid); 
    429     _set_node_geocode($node); 
     462    _geocode_node($node); 
    430463  } 
    431464}