Guest User

Pods : Pre-Save Google Geo

a guest
Dec 12th, 2012
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.62 KB | None | 0 0
  1. <?php  
  2.   add_filter('pods_api_pre_save_pod_item_life_groups', 'google_geo', 10, 2);
  3.  
  4.   function google_geo($pieces, $is_new_item) {
  5.  
  6.     // get the address values
  7.     $address1 = $pieces['fields']['meeting_location_address_line_1']['value'];
  8.     $address2 = $pieces['fields']['meeting_location_address_line_2']['value'];
  9.     $city     = $pieces['fields']['meeting_location_city']['value'];
  10.     $zip      = $pieces['fields']['meeting_location_zip_code']['value'];
  11.      
  12.     $badstrings = array (" ","'","-","<br>"); // strings we shouldn't pass in the url
  13.     $goodstrings = array ("+","",",",""); // strings we are going to replace them with
  14.      
  15.     $nice_address1 = str_replace($badstrings,$goodstrings,$address1);
  16.     $nice_address2 = str_replace($badstrings,$goodstrings,$address2);
  17.     $nice_city = str_replace($badstrings,$goodstrings,$city);
  18.     $nice_zip = str_replace($badstrings,$goodstrings,$zip);
  19.     $url = $nice_address1.",".$nice_addres2.",".$nice_city.",".$nice_zip;
  20.     $niceurl = strtolower($url);
  21.      
  22.     $geourl = "http://maps.google.com/maps/api/geocode/json?address=".$niceurl."&sensor=false";
  23.     $geoinfo = file_get_contents($geourl);
  24.     $decoded = json_decode($geoinfo);
  25.      
  26.     if ($decoded->status == "OK") :
  27.       $pieces['fields']['latitude']['value'] = $decoded->results[0]->geometry->location->lat; // copy lat into the field called lat in your pod
  28.       $pieces['fields']['longitude']['value'] = $decoded->results[0]->geometry->location->lng; // copy long into the field called long in your pod
  29.     endif;
  30.  
  31.     podsDebugger::log_debug_data(print_r($pieces, TRUE));
  32.  
  33.     return $pieces;
  34.   }
Add Comment
Please, Sign In to add comment