/**
  * Run a search query.
  * 
  * @since 1.5
  * @uses apply_filters() geo_mashup_search_query_args Filter the geo query arguments.
  *
  * @param string|array $args Search parameters.
  * @return array Search results.
  **/
 public function query($args)
 {
     $default_args = array('object_name' => 'post', 'object_ids' => null, 'exclude_object_ids' => null, 'units' => 'km', 'location_text' => '', 'radius' => null, 'sort' => 'distance_km ASC');
     $this->query_vars = wp_parse_args($args, $default_args);
     /** @var $units */
     extract($this->query_vars);
     $this->results = array();
     $this->result_count = 0;
     $this->result = null;
     $this->current_result = -1;
     $this->units = $units;
     $this->max_km = 20000;
     $this->distance_factor = 'km' == $units ? 1 : self::MILES_PER_KILOMETER;
     $this->near_location = GeoMashupDB::blank_location(ARRAY_A);
     $geo_query_args = wp_array_slice_assoc($this->query_vars, array('object_name', 'sort', 'exclude_object_ids', 'limit'));
     if (!empty($near_lat) and !empty($near_lng)) {
         $this->near_location['lat'] = $near_lat;
         $this->near_location['lng'] = $near_lng;
     } else {
         if (!empty($location_text)) {
             $geocode_text = empty($geolocation) ? $location_text : $geolocation;
             if (!GeoMashupDB::geocode($geocode_text, $this->near_location)) {
                 // No search center was found, we can't continue
                 return $this->results;
             }
         } else {
             // No coordinates to search near
             return $this->results;
         }
     }
     $radius_km = $this->max_km;
     if (!empty($radius)) {
         $radius_km = abs($radius) / $this->distance_factor;
     }
     $geo_query_args['radius_km'] = $radius_km;
     $geo_query_args['near_lat'] = $this->near_location['lat'];
     $geo_query_args['near_lng'] = $this->near_location['lng'];
     if (isset($map_cat)) {
         $geo_query_args['map_cat'] = $map_cat;
     }
     $geo_query_args = apply_filters('geo_mashup_search_query_args', $geo_query_args);
     $this->results = GeoMashupDB::get_object_locations($geo_query_args);
     $this->result_count = count($this->results);
     if ($this->result_count > 0) {
         $this->max_km = $this->results[$this->result_count - 1]->distance_km;
     } else {
         $this->max_km = $radius_km;
     }
     return $this->results;
 }
 public function reverse_geocode($lat, $lng)
 {
     if (!is_numeric($lat) or !is_numeric($lng)) {
         // Bad Request
         return new WP_Error('bad_reverse_geocode_request', __('Reverse geocoding requires numeric coordinates.', 'GeoMashup'));
     }
     $geocode_url = 'http://nominatim.openstreetmap.org/reverse?format=json&zoom=18&address_details=1&lat=' . $lat . '&lon=' . $lng . '&email=' . urlencode(get_option('admin_email'));
     $response = $this->http->get($geocode_url, $this->request_params);
     if (is_wp_error($response)) {
         return $response;
     }
     $status = $response['response']['code'];
     if ('200' != $status) {
         return new WP_Error('geocoder_http_request_failed', $status . ': ' . $response['response']['message'], $response);
     }
     $data = json_decode($response['body']);
     if (empty($data)) {
         return array();
     }
     $location = GeoMashupDB::blank_location();
     $location->lat = $lat;
     $location->lng = $lng;
     $location->address = $data->display_name;
     if (!empty($data->address)) {
         if (!empty($data->address->country_code)) {
             $location->country_code = strtoupper($data->address->country_code);
         }
         // Returns admin name in address->state, but no code
         if (!empty($data->address->county)) {
             $location->sub_admin_code = $data->address->county;
         }
         if (!empty($data->address->postcode)) {
             $location->postal_code = $data->address->postcode;
         }
         if (!empty($data->address->city)) {
             $location->locality_name = $data->address->city;
         }
     }
     return array($location);
 }
Exemplo n.º 3
0
 private function rand_location($decimal_places = 2)
 {
     $factor = pow(10, $decimal_places);
     $location = GeoMashupDB::blank_location();
     $location->lat = rand(-90 * $factor, 90 * $factor) / $factor;
     $location->lng = rand(-180 * $factor, 180 * $factor) / $factor;
     return $location;
 }
 /**
  * What to display in form
  * 
  * @access public
  * @return String
  */
 function form($args, $options)
 {
     extract($args);
     $output = "";
     if (class_exists('GeoMashupDB')) {
         global $geo_mashup_options;
         /* latest version: 1.2beta1x */
         if ($geo_mashup_options->get('overall', 'google_key')) {
             $link_url = get_bloginfo('wpurl') . '/wp-content/plugins/geo-mashup';
             # this includes the necessary scripts and other stuff for
             # geo-mashup to work
             $output = '
             <style type="text/css"> #geo_mashup_map div { margin:0; } </style>
             <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=' . $geo_mashup_options->get('overall', 'google_key') . '" type="text/javascript"></script>
             <script src="' . $link_url . '/geo-mashup-admin.js" type="text/javascript"></script>
             <script src="' . $link_url . '/JSONscriptRequest.js" type="text/javascript"></script>';
             if (!isset($geo_mashup_search)) {
                 $geo_mashup_search = "";
             }
             # Start with a blank location as we are not editing a post
             $location = GeoMashupDB::blank_location();
             $post_location_name = $location->saved_name;
             # fill up the 'location' object with values set
             # pre-preview!
             if (isset($geo_mashup_location)) {
                 list($lat, $lng) = explode(',', $geo_mashup_location);
                 $current_location = array();
                 $current_location['lat'] = trim($lat);
                 $current_location['lng'] = trim($lng);
                 $current_location['saved_name'] = $geo_mashup_location_name;
                 $current_location['geoname'] = $geo_mashup_geoname;
                 $current_location['address'] = $geo_mashup_address;
                 $current_location['postal_code'] = $geo_mashup_postal_code;
                 $current_location['country_code'] = $geo_mashup_country_code;
                 $current_location['admin_code'] = $geo_mashup_admin_code;
                 $current_location['admin_name'] = $geo_mashup_admin_name;
                 $current_location['sub_admin_code'] = $geo_mashup_sub_admin_code;
                 $current_location['sub_admin_name'] = $geo_mashup_sub_admin_name;
                 $current_location['locality_name'] = $geo_mashup_locality_name;
                 $location = (object) $current_location;
             }
             # You can use kml files to specific location, but again
             # not a feature we'll support
             $kml_url = '';
             # Saved locations using json is copied and pasted from plugin, but
             # is not supported
             $saved_locations = GeoMashupDB::get_saved_locations();
             $locations_json = '{';
             /*if ( !empty( $saved_locations ) ) {
                   $comma = '';
                   foreach ($saved_locations as $saved_location) {
                       $escaped_name = addslashes(str_replace(array("\r\n","\r","\n"),'',$saved_location->saved_name));
                       $locations_json .= $comma.'"'.$escaped_name.'":{"location_id":"'.$saved_location->id.'","name":"'.$escaped_name.
                       '","lat":"'.$saved_location->lat.'","lng":"'.$saved_location->lng.'"}';
                       $comma = ',';
                   }
               }*/
             $locations_json .= '}';
             # The code to display the map selection includes several DIVs that are
             # not wanted in TDOMF, so I simply hide them. Not the best solution
             # but easiest way to integrated with GeoMashup
             $output .= '
           <img id="geo_mashup_status_icon" src="' . $link_url . '/images/idle_icon.gif" style="float:right" />
           <label for="geo_mashup_search">' . __('Find location:', 'GeoMashup') . '
           <input id="geo_mashup_search" 
                  name="geo_mashup_search" 
                  type="text" 
                  size="35" 
                  value="' . $geo_mashup_search . '"
                  onfocus="this.select(); GeoMashupAdmin.map.checkResize();"
                  onkeypress="return GeoMashupAdmin.searchKey(event, this.value)" />
            </label>
            <select id="geo_mashup_select" name="geo_mashup_select" onchange="GeoMashupAdmin.onSelectChange(this);" style="display:none;">
                 <option>' . __('[Saved Locations]', 'GeoMashup') . '</option>
            </select>
            <a href="#" onclick="document.getElementById(\'geo_mashup_inline_help\').style.display=\'block\'; return false;">' . __('Help', 'GeoMashup') . '</a>
            <div id="geo_mashup_inline_help" style="padding:5px; border:2px solid blue; background-color:#ffc; display:none;">
                <p>' . __('Put a green pin at the location for this post.', 'tdomf') . ' ' . __('There are many ways to do it:', 'tdomf') . '
                <ul>
                 <li>' . __('Search for a location name.', 'tdomf') . '</li>
                 <li>' . __('For multiple search results, mouse over pins to see location names, and click a result pin to select that location.', 'tdomf') . '</li>
                 <li>' . __('Search for a decimal latitude and longitude, like <em>40.123,-105.456</em>.', 'tdomf') . '</li> 
                 <li>' . __('Search for a street address, like <em>123 main st, anytown, acity</em>.', 'tdomf') . '</li>
                 <li>' . __('Click on the location. Zoom in if necessary so you can refine the location by dragging it or clicking a new location.', 'tdomf') . '</li>
                </ul>
                ' . __('To execute a search, type search text into the Find Location box and hit the enter key.', 'tdomf') . '</p>
                <p>' . __('To remove the location (green pin) for a post, clear the search box and hit the enter key.', 'tdomf') . '</p>
                <p><a href="#" onclick="document.getElementById(\'geo_mashup_inline_help\').style.display=\'none\'; return false;">' . __('Close', 'tdomf') . '</a>
            </div>
            <div id="geo_mashup_map" style="width:400px;height:300px;">
                ' . __('Loading Google map. Check Geo Mashup options if the map fails to load.', 'tdomf') . '
            </div>
            <script type="text/javascript">
             //<![CDATA[
             GeoMashupAdmin.registerMap(document.getElementById("geo_mashup_map"),
                     {"link_url":"' . $link_url . '",
                      "post_lat":"' . $location->lat . '",
                      "post_lng":"' . $location->lng . '",
                      "post_location_name":"' . $post_location_name . '",
                      "saved_locations":' . $locations_json . ',
                      "kml_url":"' . $kml_url . '",
                      "status_icon":document.getElementById("geo_mashup_status_icon")});
              // ]]>
            </script>
            <label for="geo_mashup_location_name" style="display:none;">' . __('Save As:', 'GeoMashup') . '
                 <input id="geo_mashup_location_name" name="geo_mashup_location_name" type="text" maxlength="50" size="45" style="display:none;"/>
            </label>
            <input id="geo_mashup_location" name="geo_mashup_location" type="hidden" value="' . $location->lat . ',' . $location->lng . '" />
            <input id="geo_mashup_location_id" name="geo_mashup_location_id" type="hidden" value="' . $location->id . '" />
            <input id="geo_mashup_geoname" name="geo_mashup_geoname" type="hidden" value="' . $location->geoname . '" />
            <input id="geo_mashup_address" name="geo_mashup_address" type="hidden" value="' . $location->address . '" />
            <input id="geo_mashup_postal_code" name="geo_mashup_postal_code" type="hidden" value="' . $location->postal_code . '" />
            <input id="geo_mashup_country_code" name="geo_mashup_country_code" type="hidden" value="' . $location->country_code . '" />
            <input id="geo_mashup_admin_code" name="geo_mashup_admin_code" type="hidden" value="' . $location->admin_code . '" />
            <input id="geo_mashup_admin_name" name="geo_mashup_admin_name" type="hidden" value="' . $location->admin_name . '" />
            <input id="geo_mashup_sub_admin_code" name="geo_mashup_sub_admin_code" type="hidden" value="' . $location->sub_admin_code . '" />
            <input id="geo_mashup_sub_admin_name" name="geo_mashup_sub_admin_name" type="hidden" value="' . $location->sub_admin_name . '" />
            <input id="geo_mashup_locality_name" name="geo_mashup_locality_name" type="hidden" value="' . $location->locality_name . '" />
            <input id="geo_mashup_changed" name="geo_mashup_changed" type="hidden" value="" />';
         } else {
             $output = __("<p>You must configure Geo Mashup before you can use it in TDO-Mini-Forms</p>", "tdomf");
         }
     } else {
         /* old version: 1.1.2.0 */
         $geomashupOptions = get_settings('geo_mashup_options');
         if (!is_array($geomashupOptions)) {
             $geomashupOption = GeoMashup::default_options();
         }
         if ($geomashupOptions['google_key']) {
             $link_url = get_bloginfo('wpurl') . '/wp-content/plugins/geo-mashup';
             $output = '
             <style type="text/css"> #geo_mashup_map div { margin:0; } </style>
             <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=' . $geomashupOptions['google_key'] . '" type="text/javascript"></script>
             <script src="' . $link_url . '/geo-mashup-admin.js" type="text/javascript"></script>
             <script src="' . $link_url . '/JSONscriptRequest.js" type="text/javascript"></script>
             <!-- tdomf-geomashup-widget 1.0 -->';
             if (!isset($geo_mashup_search)) {
                 $geo_mashup_search = "";
             }
             # Current lat/lng
             $post_lat = '';
             $post_lng = '';
             if (!isset($geo_mashup_location)) {
                 $geo_locations = get_settings('geo_locations');
                 list($post_lat, $post_lng) = split(',', $geo_locations['default']);
             } else {
                 list($post_lat, $post_lng) = split(',', $geo_mashup_location);
             }
             # This is normally used to "save" a location, but thats not a
             # feature we'll support
             $post_location_name = '';
             # You can use kml files to specific location, but again
             # not a feature we'll support
             $kml_url = '';
             # Locations using json is copied and pasted from plugin, but
             # is not supported
             $locations_json = '{';
             if (is_array($geo_locations)) {
                 $comma = '';
                 foreach ($geo_locations as $name => $latlng) {
                     list($lat, $lng) = split(',', $latlng);
                     $escaped_name = addslashes(str_replace(array("\r\n", "\r", "\n"), '', $name));
                     if ($lat == $post_lat && $lng == $post_lng) {
                         $post_location_name = $escaped_name;
                     }
                     $locations_json .= $comma . '"' . addslashes($name) . '":{"name":"' . $escaped_name . '","lat":"' . $lat . '","lng":"' . $lng . '"}';
                     $comma = ',';
                 }
             }
             $locations_json .= '}';
             # The code to display the map selection includes several DIVs that are
             # not wanted in TDOMF, so I simply hide them. Not the best solution
             # but easiest way to integrated with GeoMashup
             $output .= '
         <img id="geo_mashup_status_icon" src="' . $link_url . '/images/idle_icon.gif" style="float:right" />
         <label for="geo_mashup_search">' . __('Find location:', 'tdomf') . '
         <input id="geo_mashup_search" 
             name="geo_mashup_search" 
             type="text" 
             size="35" 
             value="' . $geo_mashup_search . '"
             onfocus="this.select(); GeoMashupAdmin.map.checkResize();"
             onkeypress="return GeoMashupAdmin.searchKey(event, this.value);" />
         </label>
         <select id="geo_mashup_select" name="geo_mashup_select" onchange="GeoMashupAdmin.onSelectChange(this); " style="display:none;" >
             <option>' . __('[Saved Locations]', 'GeoMashup') . '</option>
         </select>            
         <a href="#" onclick="document.getElementById(\'geo_mashup_inline_help\').style.display=\'block\'; return false;">' . __('help', 'tdomf') . '</a>
         <div id="geo_mashup_inline_help" style="padding:5px; border:2px solid blue; background-color:#ffc; display:none;">
             <p>' . __('Put a green pin at the location for this post.', 'tdomf') . ' ' . __('There are many ways to do it:', 'tdomf') . '
             <ul>
                 <li>' . __('Search for a location name.', 'tdomf') . '</li>
                 <li>' . __('For multiple search results, mouse over pins to see location names, and click a result pin to select that location.', 'tdomf') . '</li>
                 <li>' . __('Search for a decimal latitude and longitude, like <em>40.123,-105.456</em>.', 'tdomf') . '</li> 
                 <li>' . __('Search for a street address, like <em>123 main st, anytown, acity</em>.', 'tdomf') . '</li>
                 <li>' . __('Click on the location. Zoom in if necessary so you can refine the location by dragging it or clicking a new location.', 'tdomf') . '</li>
             </ul>
             ' . __('To execute a search, type search text into the Find Location box and hit the enter key.', 'tdomf') . '</p>
             <p>' . __('To remove the location (green pin) for a post, clear the search box and hit the enter key.', 'tdomf') . '</p>
             <p><a href="#" onclick="document.getElementById(\'geo_mashup_inline_help\').style.display=\'none\'; return false;">' . __('close', 'tdomf') . '</a>
         </div>
         <div id="geo_mashup_map" style="width:400px;height:300px;" >
             ' . __('Loading Google map. Check Geo Mashup options if the map fails to load.', 'tdomf') . '
         </div>
         <script type="text/javascript">//<![CDATA[
             GeoMashupAdmin.registerMap(document.getElementById("geo_mashup_map"),
                 {"link_url":"' . $link_url . '",
                 "post_lat":"' . $post_lat . '",
                 "post_lng":"' . $post_lng . '",
                 "post_location_name":"' . $post_location_name . '",
                 "saved_locations":' . $locations_json . ',
                 "kml_url":"' . $kml_url . '",
                 "status_icon":document.getElementById("geo_mashup_status_icon")});
         // ]]>
         </script>
         <label for="geo_mashup_location_name" style="display:none;">' . __('Save As:', 'tdomf') . '
             <input id="geo_mashup_location_name" name="geo_mashup_location_name" type="text" size="45" style="display:none;" />
         </label>
         <input id="geo_mashup_location" name="geo_mashup_location" type="hidden" value="' . $post_lat . ',' . $post_lng . '" />';
         } else {
             $output = __("<p>You must configure Geo Mashup before you can use it in TDO-Mini-Forms</p>", "tdomf");
         }
     }
     return $output;
 }