Embedding a GMAP view into search results

Avatar-eric-london
Created by Eric.London on 2011-05-14
Tags:
New Comment
 
Please note: the content on this page orginates from ericlondon.com.
In this article, I'll explain one way to embed a GMAP view into your search results.

I downloaded and installed the following modules:

content (cck)
location_cck
gmap
gmap_location
location
location_search
views
views_ui


Configuring the GMap and Location modules:


  • Location main settings (admin/settings/location), ensure "Enable JIT geocoding" is enabled.

  • Location map links (admin/settings/location/maplinking), ensure "Google Maps" is enabled for Unites States.

  • Location Geocoding options (admin/settings/location/geocoding), enable "Google Maps" for United States.

  • GMap (admin/settings/gmap), enter your Google Maps API key.



I then imported US zip codes into my Drupal database:


$ cd sites/all/modules/contrib/location
$ mysql db_drupal < zipcodes.us.mysql


I created a content type with a single Location CCK field for address. I made this field required and set my desired collection settings. I added some sample nodes, and verified the location coordinates were being added dynamically.

Next, I added a new view with the following settings:

  • Name: search_gmap

  • Type: Node

  • Style: GMap; macro: [gmap behavior=+autozoom]; Data source: Location.module;

  • Fields: Node: Title

  • Filters: Node: Published

  • Arguments: Node: Nid; Action to take if argument is not present: display empty text; Enabled: Allow multiple terms per argument.



Lastly, I added some code to my module:

<?php
function MYMODULE_preprocess_search_results(&$vars) {
  
  // get nodeids from search results
  $node_ids = array();
  foreach ($vars['results'] as $result) {
    if (!is_object($result['node'])) {
      continue;
    }
    $node_ids[] = $result['node']->nid;
  }

  // ensure node ids exist
  if (!count($node_ids)) {
    return;
  }

  // generate views output
  $view_output = views_embed_view('search_gmap', 'default', implode('+', $node_ids));
  if ($view_output) {    
    $vars['search_results'] = $view_output . $vars['search_results'];
  }

}
?>


After flushing my caches, and running cron to index my nodes, I ran a search for "nashua":

GMap search results

Comments

 
  • Performance
    Created by Anonymous on 2011-05-16
    What sort of performance did you get on the search? Did it get better or worse with more intense queries?

    Curious about something like "Frontier Plains", or some other such query that would not filter down to the state level.
    • additional query
      Created by Eric.London on 2011-05-16
      Adding a view to standard search results will execute another query to fetch the view results.

      To clarify, this example is not showing a locative search.. just the GMap functionality. I happened to search for "Nashua" because I knew I had results for that search phrase.

      I have recently implemented Apache Solr + geospatial searching, which will be much more efficient. Blog entry coming soon :)
      • Excellent. Can't wait. I
        Created by Anonymous on 2011-05-17
        Excellent. Can't wait. I recently did some work calculating the distances between lat longs using google's geocoding API. It's a lot of fun to use, just because of the simplicity and ease with which things get done.