Selecting Results by Zip / Longitude / Latitude

I have 3 tables: 1 contains people with their city (coming from another table) and the range (int, 1-20) that they want to find in (in miles) 1 contains cities and the beginning of the zip code (i.e. BB2) 1 contains cities and their logarithms / latitudes

what I'm trying to do is select people according to the zip code that someone is entering. if this zip code is for a city within their range, then the person must be selected from the database.

I have this php code:

  // Latitude calculation
  $limit = (1 / 69.1703234283616) * $radius;
  $latitude_min = $latitude - $limit;
  $latitude_max = $latitude + $limit;

  // Longitude calculation
  $limit = (1 / (69.1703234283616 * cos($userLat * (pi/180)))) * $radius;
  $longitude_min = $longitude - $limit;
  $longitude_max = $longitude + $limit;

now the hard part. I don’t know how I am going to select people from the database, depending on what zip code was entered and their range.

can someone help me a little here?

+3
1

MySQL, - ​​ () .

DELIMITER $$

CREATE FUNCTION LAT_LNG_DISTANCE(lat1 FLOAT, lng1 FLOAT, lat2 FLOAT, lng2 FLOAT) RETURNS int(11)
BEGIN
  DECLARE latD FLOAT;
  DECLARE lngD FLOAT;
  DECLARE latS FLOAT;
  DECLARE lngS FLOAT;
  DECLARE a FLOAT;
  DECLARE c FLOAT;

  SET lat1 = RADIANS(lat1);
  SET lng1 = RADIANS(lng1);
  SET lat2 = RADIANS(lat2);
  SET lng2 = RADIANS(lng2);

  SET latD = lat2 - lat1;
  SET lngD = lng2 - lng1;

  SET latS = SIN(latD / 2);
  SET lngS = SIN(lngD / 2);

  SET a = POW(latS, 2) + COS(lat1) * COS(lat2) * POW(lngS, 2);
  SET c = 2 * ASIN(LEAST(1, SQRT(a)));

  RETURN ROUND(c * 6378137);
END;
$$
DELIMITER ;

, , . , , , , , . $var PHP; , .

SELECT l.lat, l.lng
FROM city_locations l
INNER JOIN city_postcodes c
ON l.city_id = c.city_id
WHERE c.postcode = $postcode;

, LAT_LNG_DISTANCE, .

SELECT *
FROM people p
INNER JOIN city_locations l
ON p.city_id = l.city_id
WHERE LAT_LNG_DISTANCE(l.lat, l.lng, $search_lat, $search_lng) < p.range * 1609;

1609 - , . , !

+3

Source: https://habr.com/ru/post/1719172/


All Articles