如何编写一个 mysql 查询来选择所有商店(纬度和经度) ORDER BY 离你的位置更近

How to write a mysql query that selects all stores (latitudes and longitudes) ORDER BY nearer to your position

我正在制作一个网站,我必须在其中显示列表中的所有商店,排序方式是离用户当前位置较近的商店在前,较远的商店在后。 我需要一个 MySQL 查询,该查询从数据库 ORDER BY 用户当前位置中选择所有商店。 我在 Google 上搜索了很多,但找不到任何符合我的情况的查询。

我认为你会发现你只需要从你的数据库中获取所有的商店(或者可能通过像状态这样的东西来过滤以获得更小的数据集)然后让你的应用程序处理订购他们的距离。

你可以试试这个:

SELECT temp1.* FROM (SELECT s.shop_id, s.shop_name, s.address, 3959 * ACOS( COS( RADIANS( 40.7127 ) ) * COS( RADIANS( s.latitude ) ) * COS( RADIANS( s.longitude ) - RADIANS( 74.0059 ) ) + SIN( RADIANS( 40.7127 ) ) * SIN( RADIANS( s.latitude ) ) ) AS distance FROM shop s) as temp1 WHERE temp1.distance <= 20 ORDER BY temp1.distance

它将给出 20 英里内的商店列表。
40.7127 是纬度,74.0059 是经度

您应该使用 ip 助手使用商店的 ip 地址来查找商店的(主机、国家、城镇、地区)并映射它们,以便任何人都可以找到附近的商店。你可以从 ip

找到这些
<?php
require_once("userip/ip.codehelper.io.php");

require_once("userip/php_fast_cache.php");

$_ip = new ip_codehelper();

$real_client_ip_address = $_ip->getRealIP();
$visitor_location       = $_ip->getLocation($real_client_ip_address);

$guest_ip   = $visitor_location['IP'];
$guest_country = $visitor_location['CountryName'];
$guest_city  = $visitor_location['CityName'];
$guest_state = $visitor_location['RegionName'];

echo "IP Address: ". $guest_ip. "<br/>";
echo "Country: ". $guest_country. "<br/>";
echo "State: ". $guest_state. "<br/>";
echo "City: ". $guest_city. "<br/>";

?>

您还可以找到

$ip             = $visitor_location['IP'];
$Continent_Code     = $$$visitor_location['ContinentCode'];
$Continent_Name     = $visitor_location['ContinentName'];
$Country_Code2      = $visitor_location['CountryCode2'];
$Country_Code3      = $visitor_location['CountryCode3'];
$Country        = $visitor_location['Country'];
$Country_Name       = $visitor_location['CountryName'];
$State_Name         = $visitor_location['RegionName'];
$City_Name      = $visitor_location['CityName'];
$City_Latitude      = $visitor_location['CityLatitude'];
$City_Longitude     = $visitor_location['CityLongitude'];
$Country_Latitude   = $visitor_location['CountryLatitude'];
$Country_Longitude  = $visitor_location['CountryLongitude'];
$Country_Longitude  = $visitor_location['CountryLongitude'];
$LocalTimeZone      = $visitor_location['LocalTimeZone'];
$Calling_Code       = $visitor_location['CallingCode'];
$Population         = $visitor_location['Population'];
$Area_SqKm      = $visitor_location['AreaSqKm'];
$Capital        = $visitor_location['Capital'];
$Electrical         = $visitor_location['Electrical'];
$Languages      = $visitor_location['Languages'];
$Currency       = $visitor_location['Currency'];
$Flag           = $visitor_location['Currency'];