0
votes

Spent a few hours on this now and it just doesn't seem to work. A Listener hits my site and I grab their ip and ultimately there approximate latitude and longitude. I have a table of my Radio stations with their approximate latitudes and longitudes. I simply want to see if the new Listener is within 25 miles of one of my stations so I can give them that feed. If not, I will give them my global stream.

When I use the query below,

//Connect to database
// CONNECT TO JOOMLA DATABASE
$db = JFactory::getDbo();

// Create a new query object.
$query = $db->getQuery(true);

$query
    ->select($db->quoteName(array('cityName', 'countryCode', 'latitude', 'longitude')))
    ->select($db->quote('( 3959 * acos( cos( radians('.$latitude.') ) * cos( radians( latitude ) ) 
    * cos( radians( longitude ) - radians('.$longitude.') ) + sin( radians('.$latitude.') )
    * sin( radians( latitude ) ) ) )', 'distance'))
    ->from($db->quoteName('#__areas'))
    ->having($db->quote('distance') . ' < ' . $miles)
    ->order($db->quote('distance') . ' DESC')
    ->setLimit('1');

// Reset the query using our newly populated query object.
$db->setQuery($query);

// Load the results as a list of stdClass objects (see later for more options on retrieving data).
$row = $db->loadObject();

echo $distance = distance($latitude,$longitude,$row->latitude,$row->longitude,"M");
echo '<br />';
print_r($row);
    exit;

The output is:

stdClass Object ( [cityName] => Fort Lauderdale [countryCode] => US [latitude] => 26.122299 [longitude] => -80.143402 [( 3959 * acos( cos( radians(46.8898) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians(-114.031) ) + sin( radians(46.8898) ) * sin( radians( latitude ) ) ) )] => ( 3959 * acos( cos( radians(46.8898) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians(-114.031) ) + sin( radians(46.8898) ) * sin( radians( latitude ) ) ) ) )

Should not the result of the long calculation be a distance instead of a repeat of the calculation? I've tried about every combination I can think of except the right one. Could really use some help. Thanks!

1
so just comment your print_r($row);Alex

1 Answers

0
votes

try just :

->select('( 3959 * acos( cos( radians('.$latitude.') ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians('.$longitude.') ) + sin( radians('.$latitude.') ) * sin( radians( latitude ) ) ) ) as distance'))