0

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

4

3 回答 3

1

你可以试试这个:

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 是经度

于 2015-03-18T05:36:52.820 回答
0

我认为您会发现您只需要从数据库中获取所有存储(或可能通过状态等过滤以生成更小的数据集),然后让您的应用程序处理按距离对它们进行排序。

于 2015-03-18T05:26:20.763 回答
0

您应该使用 ip helper 使用商店的 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'];
于 2015-03-18T06:06:59.220 回答