17

我有$latitude = 29.6815400and $longitude = 64.3647100,现在在 MySQL 中,我想将 15 个最近的地方带到这些坐标,我打算做这个查询:

SELECT *
FROM places
WHERE latitude  BETWEEN($latitude  - 1, $latitude  + 1)
AND   longitude BETWEEN($longitude - 1, $logintude + 1)
LIMIT 15;

您认为它是正确的还是您提出其他建议?

怎么办BEETWEEN,因为我想在附近的地方搜索最多 50Km 范围的槽?

我忘了说我也可以在运行查询之前使用 PHP 做任何事情。

注意:我不能使用存储过程

4

4 回答 4

19

这是计算两点之间距离PHP公式:

function getDistanceBetweenPointsNew($latitude1, $longitude1, $latitude2, $longitude2, $unit = 'Mi') 
{
   $theta = $longitude1 - $longitude2;
   $distance = (sin(deg2rad($latitude1)) * sin(deg2rad($latitude2))+
               (cos(deg2rad($latitude1)) * cos(deg2rad($latitude2)) * cos(deg2rad($theta)));
   $distance = acos($distance); $distance = rad2deg($distance); 
   $distance = $distance * 60 * 1.1515;

   switch($unit) 
   { 
     case 'Mi': break;
     case 'Km' : $distance = $distance * 1.609344; 
   } 
   return (round($distance,2)); 
}

然后添加一个查询以获取距离小于或等于上述距离的所有记录:

$qry = "SELECT * 
        FROM (SELECT *, (((acos(sin((".$latitude."*pi()/180)) *
        sin((`geo_latitude`*pi()/180))+cos((".$latitude."*pi()/180)) *
        cos((`geo_latitude`*pi()/180)) * cos(((".$longitude."-
        `geo_longitude`)*pi()/180))))*180/pi())*60*1.1515*1.609344) 
        as distance
        FROM `ci_geo`)myTable 
        WHERE distance <= ".$distance." 
        LIMIT 15";

您可以在这里查看类似的计算。

你可以在这里阅读更多

更新:

您必须记住,要计算longitude2longitude2您需要知道:

每个纬度相距约69 英里(111 公里)。范围从赤道的 68.703 英里(110.567 公里)到两极的 69.407(111.699 公里)不等(由于地球略呈椭圆形)。这很方便,因为每分钟(1/60 度)大约是一英里。

一个经度在赤道处最宽,为69.172 英里 (111.321),并在两极逐渐缩小至零。在北纬 40° 或南纬 40° 经度之间的距离为 53 英里(85 公里)。

所以$longitude2 $latitude2按照50km计算的话大概:

$longitude2 = $longitude1 + 0.449; //0.449 = 50km/111.321km
$latitude2 = $latitude1 + 0.450; // 0.450 = 50km/111km
于 2013-01-10T10:07:34.877 回答
17

您必须考虑用繁重的查询淹没任何像 MySQL 这样的 DBMS 不应该是最好的解决方案。

相反,您可以推测一个非常快速的 SQL 查询,选择坐标在 side 的简单正方形内的所有位置$radius,而不是突然选择一个完美的圆半径。PHP可以过滤多余的。

让我展示一下这个概念:

$lat    = 45.0.6072;
$lon    = 7.65678;
$radius = 50; // Km

 // Every lat|lon degree° is ~ 111Km
$angle_radius = $radius / ( 111 * cos( $lat ) );

$min_lat = $lat - $angle_radius;
$max_lat = $lat + $angle_radius;
$min_lon = $lon - $angle_radius;
$max_lon = $lon + $angle_radius;

$results = $db->getResults("... WHERE latitude BETWEEN $min_lat AND $max_lat AND longitude BETWEEN $min_lon AND $max_lon"); // your own function that return your results (please sanitize your variables)

$filtereds = [];
foreach( $results as $result ) {
    if( getDistanceBetweenPointsNew( $lat, $lon, $result->latitude, $result->longitude, 'Km' ) <= $radius ) {
        // This is in "perfect" circle radius. Strip it out.
        $filtereds[] = $result;
    }
}

// Now do something with your result set
var_dump( $filtereds );

通过这种方式,MySQL 运行了一个非常友好的查询,不需要全表扫描,而 PHP 使用类似于getDistanceBetweenPointsNew()此页面中发布的函数的功能去除剩余部分,比较从结果集的坐标到半径中心的距离.

为了不浪费(大)性能增益,请在数据库中索引您的坐标列。

快乐黑客!

于 2015-03-03T21:36:42.177 回答
2

我对销售房屋应用程序做了类似的事情,按距给定点的距离排序,将其放在您的 SQL 选择语句中:

((ACOS(SIN(' . **$search_location['lat']** . ' * PI() / 180) * SIN(**map_lat** * PI() / 180) + COS(' . **$search_location['lat']** . ' * PI() / 180) * COS(**map_lat** * PI() / 180) * COS((' . **$search_location['lng']** . ' - **map_lng**) * PI() / 180)) * 180 / PI()) * 60 * 1.1515) AS "distance"

替换$search_location为相关的 lat/lng 值,map_lat/map_lng 值是包含 lat/lng 值的 SQL 列。然后,您可以按距离对结果进行排序,并使用 where 或 have 子句过滤 50 公里范围内的属性。

如果您需要分页等附加功能,我建议您使用 SQL 作为与 PHP 相比的方法。

于 2013-01-10T14:03:38.967 回答
0

有点晚了,但它可能会对某人有所帮助 - 如果您想要按位置最近的城市,我不会继续距离,因为那样一个孤立的位置将无法检索任何东西。试试这个:

$G_how_close_to_find_cities = "1.1"; // e.g. 1.1 = 10% , 1.2=20% etc
$G_how_many_cities_to_find_by_coordinates = "10";
$query = "SELECT * from Cities  WHERE 
                        Cities__Latitude <= ('".$latitude*$G_how_close_to_find_cities."') AND Cities__Latitude >= ('".$latitude/$G_how_close_to_find_cities."') 
                    AND Cities__Longitude <= ('".$longitude*$G_how_close_to_find_cities."') AND Cities__Longitude >= ('".$longitude/$G_how_close_to_find_cities."') 
                    ORDER BY SQRT(POWER((Cities__Latitude - ".$latitude."),2)+POWER((Cities__Longitude - ".$longitude."),2)) LIMIT 0,".$G_how_many_cities_to_find_by_coordinates;
于 2017-03-14T20:55:42.390 回答