0

I was trying to solve this but seems like i cant get the right thing to do in WHERE SQL. HERE's my code. PLease help me solve this problem

$url = "https://maps.googleapis.com/maps/api/geocode/json?address=$string&key=AIzaSyAdk7l9UtPkZsm2FzL465yMXeFDATEubp4";

$result_string = file_get_contents($url);
 $result = json_decode($result_string, true);


$latitudeFrom = $result['results'][0]['geometry']['location']['lat'];
$longitudeFrom = $result['results'][0]['geometry']['location']['lng'];


$theta = $longitudeFrom - $longitudeTo;
$dist = sin(deg2rad($latitudeFrom)) * sin(deg2rad($latitudeTo)) +  cos(deg2rad($latitudeFrom)) * cos(deg2rad($latitudeTo)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$miles = $dist * 60 * 1.1515;

$distance = ($miles * 1.609344).' km';





$query->select('*')->from('#__bbe_listing');


>>>> Below is where i get wrong for sure..


$query->where((($dist =sin(deg2rad($latitudeFrom)) * sin(deg2rad('latitude')) + cos(deg2rad($latitudeFrom)) * cos(deg2rad('latitude')) * cos(deg2rad($longitudeFrom - 'longitude'))).($dist = acos($dist)).($dist = rad2deg($dist)) ($miles = $dist * 60 * 1.1515).($distance = ($miles * 1.609344))). $distance < 12);
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345

1 Answers1

0

You can create haversine store fucntion in database

DELIMITER $$
DROP FUNCTION IF EXISTS haversine$$

CREATE FUNCTION haversine(
        lat1 FLOAT, lon1 FLOAT,
        lat2 FLOAT, lon2 FLOAT
     ) RETURNS FLOAT
    NO SQL DETERMINISTIC
    COMMENT 'Returns the distance in degrees on the Earth
             between two known points of latitude and longitude'
BEGIN
    RETURN DEGREES(ACOS(
              COS(RADIANS(lat1)) *
              COS(RADIANS(lat2)) *
              COS(RADIANS(lon2) - RADIANS(lon1)) +
              SIN(RADIANS(lat1)) * SIN(RADIANS(lat2))
            ));
END$$

DELIMITER ;

Usage:

To use this function successfully, you need to know the following things:

  • the latitude and longitude of a starting point
  • the latitude and longitude of an ending point
  • the number of kilometers (111.045), statute miles (69), or nautical miles (60) in a degree of latitude.

For example, you can use this stored haversine function to look in a zip code table and find the fifteen closest points to a particular point.

SELECT zip, primary_city, latitude, longitude,
       111.045*haversine(latitude,longitude,latpoint, longpoint) AS distance_in_km
 FROM zip
 JOIN (
     SELECT  42.81  AS latpoint,  -70.81 AS longpoint
   ) AS p
 ORDER BY distance_in_km
 LIMIT 15

Notice that the value of the haversine function is returned in degrees of longitude. Multiplying that value by 111.045 converts it to kilometers.

In your case:


$url = "https://maps.googleapis.com/maps/api/geocode/json?address=$string&key=AIzaSyAdk7l9UtPkZsm2FzL465yMXeFDATEubp4";

$result_string = file_get_contents($url);
 $result = json_decode($result_string, true);


$latitudeFrom = $result['results'][0]['geometry']['location']['lat'];
$longitudeFrom = $result['results'][0]['geometry']['location']['lng'];


$query->select('*')->from('#__bbe_listing');


$query->where(60*haversine($latitudeFrom,$longitudeFrom,latitude,longitude) < 12);

Ronak Dhoot
  • 2,322
  • 1
  • 12
  • 19