Tribe__Events__Pro__Geo_Loc::get_venues_in_geofence( float $lat, float $lng, float $geofence_radius = null )
Get a list of venues inside a given geo fence with the given geo point at the center.
Contents
Parameters
- $lat
-
(float) (Required)
- $lng
-
(float) (Required)
- $geofence_radius
-
(float) (Optional)
Default value: null
Return
(array|null)
Source
File: src/Tribe/Geo_Loc.php
public function get_venues_in_geofence( $lat, $lng, $geofence_radio = null ) {
$lat = floatval( $lat );
if ( empty( $geofence_radio ) ) {
$geofence_radio = $this->get_geofence_size();
}
// get the limits of the geofence
$maxLat = $lat + rad2deg( $geofence_radio / self::EARTH_RADIO );
$minLat = $lat - rad2deg( $geofence_radio / self::EARTH_RADIO );
$maxLng = $lng + rad2deg( $geofence_radio / self::EARTH_RADIO / cos( deg2rad( $lat ) ) );
$minLng = $lng - rad2deg( $geofence_radio / self::EARTH_RADIO / cos( deg2rad( $lat ) ) );
$latlng = array(
'lat' => $lat,
'lng' => $lng,
'minLat' => $minLat,
'maxLat' => $maxLat,
'minLng' => $minLng,
'maxLng' => $maxLng,
);
/**
* Allow overriding of Venues query by returning an array of Venue IDs.
*
* @since 4.4.16
*
* @param null|int[] $venues Venue IDs, default null will query database directly.
* @param array $latlng {
* Latitude / longitude values for geofencing
*
* @type float $lat Central latitude point
* @type float $lng Central longitude point
* @type float $minLat Minimum latitude constraint
* @type float $maxLat Maximum latitude constraint
* @type float $minLng Minimum longitude constraint
* @type float $maxLng Maximum longitude constraint
* }
* @param float $geofence_radio Geofence size in kilometers
*/
$venues = apply_filters( 'tribe_geoloc_pre_get_venues_in_geofence', null, $latlng, $geofence_radio );
if ( null === $venues ) {
global $wpdb;
// Get the venues inside a geofence
$sql = "
SELECT DISTINCT venue_id FROM (
SELECT coords.venue_id,
MAX( coords.lat ) AS lat,
MAX( coords.lng ) AS lng
FROM (
SELECT post_id AS venue_id,
CASE
WHEN meta_key = %s
THEN meta_value
END AS lat,
CASE
WHEN meta_key = %s
THEN meta_value
END AS lng
FROM $wpdb->postmeta
WHERE
meta_key = %s
OR meta_key = %s
) AS coords
INNER JOIN $wpdb->posts p
ON p.id = coords.venue_id
WHERE
(lat > %f OR lat IS NULL)
AND (lat < %f OR lat IS NULL)
AND (lng > %f OR lng IS NULL)
AND (lng < %f OR lng IS NULL)
AND p.post_status = 'publish'
AND p.post_type = %s
GROUP BY venue_id
HAVING
lat IS NOT NULL
AND lng IS NOT NULL
) AS query
";
$sql = $wpdb->prepare(
$sql,
array(
self::LAT,
self::LNG,
self::LAT,
self::LNG,
$minLat,
$maxLat,
$minLng,
$maxLng,
Tribe__Events__Main::VENUE_POST_TYPE,
)
);
$venues = $wpdb->get_col( $sql );
}
// Return null if $venues is empty
if ( empty( $venues ) ) {
$venues = null;
}
return $venues;
}