【问题标题】:Search for item within distance of user (geo-location) PHP/MySQL [duplicate]搜索用户距离内的项目(地理位置)PHP / MySQL [重复]
【发布时间】:2016-05-17 04:08:28
【问题描述】:

我有一个包含项目、纬度和经度等的 mySQL 表。
我将如何根据当前地理位置在 KM 中搜索特定数量内的项目?

示例:
用户从“20KM”下拉框中选择一个值
如何根据地理位置获取 20KM 距离内的所有物品?

布局截图如下:

【问题讨论】:

  • 空中距离还是公路距离?找到您所在位置附近的位置很重要。
  • @hosseinbarzegari 公路距离。
  • 最好的解决方案是使用 google api ,它可以帮助你并更新它的路径。尽管我向您发送了带有源代码的答案示例代码。 ;)
  • 好的,谢谢,稍后会查看。

标签: php mysql geolocation


【解决方案1】:
SELECT *,3956*2*ASIN(SQRT(POWER(SIN((19.286558 - latitude)*pi()/180/2),2)+COS(19.286558 * pi()/180)
*COS(latitude * pi()/180)*POWER(SIN((-99.612494 -longitude)* pi()/180/2),2)))
as distance FROM table  having distance < 10 ORDER BY distance;

这将为您提供 10 公里范围内的记录。在have子句中修改20米的查询。

【讨论】:

    【解决方案2】:

    最好使用google示例代码。

    此链接可以帮助您:

    http://code.google.com/apis/maps/articles/phpsqlsearch.html

    他们的 PHP 和 SQL 查询帮助非常好。

    添加:

    this code 也可能有帮助:

    function distance($lat1, $lon1, $lat2, $lon2, $unit) {
    
      $theta = $lon1 - $lon2;
      $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +  cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
      $dist = acos($dist);
      $dist = rad2deg($dist);
      $miles = $dist * 60 * 1.1515;
      $unit = strtoupper($unit);
    
      if ($unit == "K") {
          return ($miles * 1.609344);
      } else if ($unit == "N") {
          return ($miles * 0.8684);
      } else {
          return $miles;
      }
    }
    

    结果:

    echo distance(32.9697, -96.80322, 29.46786, -98.53506, "M") . " Miles<br>";
    echo distance(32.9697, -96.80322, 29.46786, -98.53506, "K") . " Kilometers<br>";
    echo distance(32.9697, -96.80322, 29.46786, -98.53506, "N") . " Nautical Miles<br>";
    

    【讨论】:

      【解决方案3】:

      您可以计算当前位置与所有公园之间的海峡距离,并仅过滤匹配的距离标准。

          /**
           * @param float $parkLatitude
           * @param float $parkLongitude
           * @param float $userLatitude
           * @param float $userLongitude
           * @param float $distance in km
           *
           * @return bool
           */
          function isParkInDistance($parkLatitude, $parkLongitude, $userLatitude, $userLongitude, $distance)
      {
          $earthRadius = 6371; //km
      
          $dLat = deg2rad($userLatitude - $parkLatitude);
          $dLon = deg2rad($userLongitude - $parkLongitude);
      
          $a = sin($dLat/2) * sin($dLat/2) + cos(deg2rad($parkLatitude)) * cos(deg2rad($userLatitude)) * sin($dLon/2) * sin($dLon/2);
          $c = 2 * asin(sqrt($a));
          $d = $earthRadius * $c;
      
          return $d <= $distance;
      }
      

      或者您可以使用 this google service 之类的东西来计算通过道路的距离

      【讨论】:

        猜你喜欢
        • 2011-08-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-11
        • 2012-07-18
        • 1970-01-01
        相关资源
        最近更新 更多