【问题标题】:SQL query, select nearest places by a given coordinates [duplicate]SQL查询,通过给定坐标选择最近的地方[重复]
【发布时间】:2012-12-24 15:31:17
【问题描述】:

我有 $latitude = 29.6815400$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 做任何事情。

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

【问题讨论】:

  • 您确定会在这些条件下获得 15 条记录吗?
  • @mamdouhalramadan nope ,我问这是否正确
  • 然后。确定这是不正确的。因为这些条件会根据固定条件为您提供数据库中的内容,而您需要的是动态方法。就像使用欧几里得距离一样。但这需要存储过程!!!
  • 这正是您所需要的。但最好在您的情况下使用存储过程。或者如果你使用 php 来做它会很棒。

标签: php mysql sql range coordinates


【解决方案1】:

这是计算两点之间距离的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";

您可以查看here 进行类似的计算。

你可以阅读更多here

更新:

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

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

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

所以按照50km计算$longitude2 $latitude2然后大概:

$longitude2 = $longitude1 + 0.449; //0.449 = 50km/111.321km
$latitude2 = $latitude1 + 0.450; // 0.450 = 50km/111km

【讨论】:

  • 这不需要存储过程吧? :P
  • 不,这不需要存储过程。
  • 太棒了,我稍后会测试一下性能基准,我会告诉你或直接接受答案!
  • 更新了查询以适应 KM 的情况。有一些错误修改:)
  • 您应该有一个 ORDER BY 以按距离升序返回结果。
【解决方案2】:

您必须考虑用繁重的查询淹没任何 DBMS(如 MySQL)不应该是最佳解决方案。

相反,您可以推测一个非常快速的 SQL 查询选择所有坐标在边 $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() 函数的东西去除剩余部分,比较与结果集坐标的距离到你的半径的中心。

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

黑客愉快!

【讨论】:

  • 您的解决方案非常快,但有一个小错误。对于经度,角半径取决于纬度:要计算 $min_lon 和 $max_lon,使用 $angle_radius = $radius / (111*cos($lat));
  • 感谢您发布出色的实用答案
  • 完全同意@EricJOYÉ 使用cos($lat) 的计算,这不是一个小错误,而是一个大错误。我在 50 公里中得到 15 公里的差异。这是完全不能接受的。
  • 感谢指正。
  • @IgniteCoders 好吧,这取决于查询的复杂性和结果集的维度。最重要的是避免数据库全表扫描,使用这种方法可以避免它。
【解决方案3】:

我在一个卖房应用上做了类似的事情,按距给定点的距离排序,把它放在你的 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 替换为您的相关纬度/经度值,map_lat/map_lng 值是包含经度/经度值的 SQL 列。然后,您可以按距离对结果进行排序,并使用 where 或 have 子句过滤 50 公里范围内的属性。

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

【讨论】:

  • 不错,但现在的主要问题是设置 50 公里示例范围:P
【解决方案4】:

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

$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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-28
    • 1970-01-01
    • 2014-01-17
    • 2019-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多