Advertisement
dangavin

Geolocation

Sep 20th, 2013
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.19 KB | None | 0 0
  1. // Geolocate on post save
  2. function dcom_geolocate_locations($post_id){
  3.     global $post;
  4.     if(get_post_type() == 'dcom_auto_locations'){
  5.       $fields = false;
  6.  
  7.       // load from post
  8.       if( isset($_POST['fields']) )
  9.       {
  10.           $fields = $_POST['fields'];
  11.           //'<pre>'.var_dump($fields).'</pre>';
  12.           $address = '';
  13.           foreach($fields as $key=>$value){
  14.               if($key == 'field_523c578bad0ed' || $key == 'field_523c57a5ad0ee'){
  15.                     $address .= $value.' ';
  16.               }
  17.           }
  18.       }
  19.  
  20.       /* Request passes all checks; update the post's metadata */
  21.         if(!empty($address)){
  22.             $location = $address;
  23.             $geocode = file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.urlencode($address).'&sensor=false');
  24.             $output = json_decode($geocode);
  25.             if($output->status == 'OK'){
  26.                 $latitude = $output->results[0]->geometry->location->lat;
  27.                 $longitude = $output->results[0]->geometry->location->lng;
  28.                     if($latitude){
  29.                         update_post_meta($post->ID, 'dcom_auto_latitude', $latitude);
  30.                     }
  31.                     if($longitude){
  32.                         update_post_meta($post->ID, 'dcom_auto_longitude', $longitude);
  33.                     }
  34.             }
  35.         }
  36.  
  37.     }
  38. }
  39. add_action('save_post', 'dcom_geolocate_locations');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement