计算 PHP 循环中多个 Lat/Long 之间的多个距离
Calculate Multiple Distance Between Multiple Lat/Long in PHP Loop
我有以下两个 Driver_position
和 user_position
table 正在更新用户和驱动程序 lat/long 的位置。我需要为 user_position 5 公里附近的每个 user_id 找出 driver_id's,这样结果就像 user_id1 = driver_id1,driver_id2,driver_id3 etc
For user_id2=driver_id5
到目前为止,我有以下代码来计算以公里为单位的距离。我怎样才能在 PHP
中实现上述目标
$lat1 = deg2rad($user_latitude);
$lon1 = deg2rad($user_longitude);
$lat2 = deg2rad($driver_latitude);
$lon2 = deg2rad($driver_longitude);
$latDelta = $lat1 - $lat2;
$lonDelta = $lon1 - $lon2;
$earthRadius=3959;
$angle = 2 * asin(sqrt(pow(sin($latDelta / 2), 2) +
cos($lat2) * cos($lat1) * pow(sin($lonDelta / 2), 2)));
//return $angle * $earthRadius;
$dis= ($angle * $earthRadius)/0.62137; // miles to km conversion mi / 0.62137 = km
您可以使用以下 Mysql 查询您的解决方案
// To search by kilometers instead of miles, replace 3959 with 6371
SELECT user_position.*,driver_position.driver_id, ( 3959 * acos( cos( radians(37.4219983) ) * cos( radians( driver_position.latitude ) ) * cos( radians( driver_position.longitude ) - radians(-122.0844) ) + sin( radians(37.4219983) ) * sin(radians(driver_position.latitude)) ) ) AS distance FROM user_position ,driver_position where user_position.user_id=10002 HAVING distance < 10000
希望对您有所帮助。
是的,您可以根据需要传递动态值。检查你下面给定的代码
$pickup_latitude='37.4219983'; $pickup_longitude='-122.084';
$sql="(SELECT *,driver_location.driver_id, ( 6371 * acos( cos( radians('".$pickup_latitude."') ) * cos( radians( driver_location.latitude ) ) * cos(radians( driver_location.longitude ) - radians('".$pickup_longitude."') ) + sin( radians('".$pickup_latitude."') ) * sin( radians( driver_location.latitude ) ) ) ) as distance FROM driver_location where driver_location.status='Online' HAVING distance >=0)"
您正在传递带有单引号的 $pickup_longitude 变量值,因此它会将值视为“$pickup_longitude”。您需要使用双引号传递其值,例如 '".$pickup_longitude."'
试试上面的代码。
我有以下两个 Driver_position
和 user_position
table 正在更新用户和驱动程序 lat/long 的位置。我需要为 user_position 5 公里附近的每个 user_id 找出 driver_id's,这样结果就像 user_id1 = driver_id1,driver_id2,driver_id3 etc
For user_id2=driver_id5
到目前为止,我有以下代码来计算以公里为单位的距离。我怎样才能在 PHP
中实现上述目标$lat1 = deg2rad($user_latitude);
$lon1 = deg2rad($user_longitude);
$lat2 = deg2rad($driver_latitude);
$lon2 = deg2rad($driver_longitude);
$latDelta = $lat1 - $lat2;
$lonDelta = $lon1 - $lon2;
$earthRadius=3959;
$angle = 2 * asin(sqrt(pow(sin($latDelta / 2), 2) +
cos($lat2) * cos($lat1) * pow(sin($lonDelta / 2), 2)));
//return $angle * $earthRadius;
$dis= ($angle * $earthRadius)/0.62137; // miles to km conversion mi / 0.62137 = km
您可以使用以下 Mysql 查询您的解决方案
// To search by kilometers instead of miles, replace 3959 with 6371
SELECT user_position.*,driver_position.driver_id, ( 3959 * acos( cos( radians(37.4219983) ) * cos( radians( driver_position.latitude ) ) * cos( radians( driver_position.longitude ) - radians(-122.0844) ) + sin( radians(37.4219983) ) * sin(radians(driver_position.latitude)) ) ) AS distance FROM user_position ,driver_position where user_position.user_id=10002 HAVING distance < 10000
希望对您有所帮助。
是的,您可以根据需要传递动态值。检查你下面给定的代码
$pickup_latitude='37.4219983'; $pickup_longitude='-122.084';
$sql="(SELECT *,driver_location.driver_id, ( 6371 * acos( cos( radians('".$pickup_latitude."') ) * cos( radians( driver_location.latitude ) ) * cos(radians( driver_location.longitude ) - radians('".$pickup_longitude."') ) + sin( radians('".$pickup_latitude."') ) * sin( radians( driver_location.latitude ) ) ) ) as distance FROM driver_location where driver_location.status='Online' HAVING distance >=0)"
您正在传递带有单引号的 $pickup_longitude 变量值,因此它会将值视为“$pickup_longitude”。您需要使用双引号传递其值,例如 '".$pickup_longitude."'
试试上面的代码。