compact location search form

Events happening in the community are now at Drupal community events on www.drupal.org.
fatfish's picture

Hi

Im trying to build a compact "search by location" block that contains two fields:
1. key word - node's title / body / taxonomy / CCK
2. location - single text field for location like "Italy" or "5th road New York US"

I'm looking for the best approch for this perpose, any idaes?
Dose someone know of any project like this out there

AttachmentSize
search_block.jpg10.31 KB

Comments

Found a solution

fatfish's picture

I found an integrated solution with a little code writing.
I have created a view with two exposed filters:
Search: Search Terms
Location: Distance / Proximity

(Im using exposed fields for the resone I could no use 'Search: Search Terms' as an argument, by the end of this proccess, the exposed filters can be turned invisible by CSS)

Now I have a search form on top of the view with key word, latitude and longitude, distance and units. Too much information!

I have created a new block I can show on top of every page:

function myproject_block($op = 'list', $delta = 0, $edit = array()) {
  switch ($op) {
case 'list':
     $blocks[0] = array('info' => t('search by location'));
      return $blocks;
    case 'view':
     switch ($delta) {
      case 0:
            $output = drupal_get_form('myproject_search_by_location_form');
          $block = array(
            'content' => $output,
         );
         break;
     }
  }
  return $block;
}

function myproject_search_by_location_form() {
   $form['what'] = array(
       "#type" => "textfield",
     "#title" => t('What'),
      "#maxlength" => 255,
      "#size" => 20,
        "#required" => false,
     '#weight' => 5,
   );
$form['where'] = array(
      "#type" => "textfield",
     "#title" => t('Where'),
     "#maxlength" => 255,
      "#size" => 20,
        "#required" => false,
     '#weight' => 10,
  );
$form['submit'] = array(
     "#type" => "submit",
        "#value" => t('Find now'),
      '#weight' => 15,
  );
return $form;
}

The submit function sould take the two parameter, transform them into keyword, latitude ad longitude. and then redirect back to the view (the path for the view page is 'location_search/views')

function myproject_search_by_location_form_submit($form, &$form_state) {
    $param['keys'] = null;
   $param['distance[latitude]'] = null;
$param['distance[longitude]'] =null;
$param['distance[distance]'] = null;
$param['distance[search_distance]'] = null;
  $param['distance[search_units]'] = 'mile';
if($form_state["values"]["what"]) {
        $param['keys'] = $form_state["values"]["what"];
  }
  if($form_state["values"]["where"]) {
       $latlong = myproject_string_to_latlog($form_state["values"]["where"]);
     if($latlong) {
         $param['distance[latitude]'] = $latlong['lat'];
            $param['distance[longitude]'] = $latlong['lon'];
           $param['distance[distance]'] = 25;
           $param['distance[search_distance]'] = 25;
            $param['distance[search_units]'] = 'mile';
     }
  }
  drupal_goto('location_search/views',$param);
}

function myproject_string_to_latlog($query) {
// code borowed from location/geocoding/google.inc at location module
  $key = variable_get('location_geocode_google_apikey', '');
if (function_exists('gmap_get_key')) {
       $key = gmap_get_key();
}

$service_url = 'http://maps.google.com/maps/geo?output=xml&key='. $key .'&q=';
$http_reply = drupal_http_request($service_url . urlencode($query));
   $status_code_match = array();
  preg_match('/<code>(.)<\/code>/', $http_reply->data, $status_code_match);
    $status_code = $status_code_match[1];
  if ($status_code != 200) {
     return NULL;
   }
  $accuracy_code_match = array();
    preg_match('/Accuracy="([0-9])"/', $http_reply->data, $accuracy_code_match);
    $accuracy_code = $accuracy_code_match[1];
  if ($accuracy_code < 3) {
       return NULL;
   }

$latlon_match = array();
   preg_match('/<coordinates>(.
)<\/coordinates>/', $http_reply->data, $latlon_match);

  $latlon_exploded = explode(',', $latlon_match[1]);

  return array('lat' => $latlon_exploded[1], 'lon' => $latlon_exploded[0]);
}

Please test it and comment it if you wish. What I like to know is how can this feature can packed as a module that includes this specific view.

I'm trying to take the

fumbling's picture

I'm trying to take the standard Drupal search results page and have it display through a block, but a block that comes from a view that I can control. Is your code above useful for that? Sounds like it might be.

Location and Mapping

Group organizers

Group categories

Wiki type

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds:

Hot content this week