简体   繁体   English

如何使用php使用经度和纬度获取点之间的距离?

[英]how to get distance between points using latitude and longitude using php?

I am trying to get distance between two location. 我试图获得两个位置之间的距离。

Here one location is user input and another location is my store location. 这里一个位置是用户输入,另一个位置是我的商店位置。 I want to show the the distance between there input location to my store location. 我想显示输入位置到我的商店位置之间的距离。

To get user location we use form, where user share there zip code and we convert that zip to longitude and latitude. 为了获得用户位置,我们使用表格,用户在其中共享邮政编码,然后将其转换为经度和纬度。 And in my data base i stored the store location and its respective longitude and latitude. 然后在我的数据库中存储了商店位置及其各自的经度和纬度。

Here in this case can any one help me to get distance. 在这种情况下,任何人都可以帮助我拉开距离。

Thanks 谢谢

GeoDataSource team develop a very useful function to achieve this: GeoDataSource团队开发了一个非常有用的功能来实现此目的:

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

Reference 参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用于查找使用纬度和经度之间的距离的Sql返回错误的距离 - Sql for finding the distance between using latitude and longitude returns wrong distance 使用 PHP 和谷歌地图 Api 计算出 2 个纬度和经度之间的距离 - Using PHP and google Maps Api to work out distance between 2 latitude and longitude Laravel中两个位置之间的距离在数据库查询中使用经纬度 - Distance between two locations in Laravel using latitude and longitude in database query 如何在两对纬度经度之间以几米为单位获得距离 - How to get distance in few meters between 2 pair of latitude longitude 使用php、API自动获取经纬度 - Get latitude and longitude automatically using php, API 如何在 PHP 中使用纬度和经度获取邮政编码 - How to get ZIP code using latitude and longitude in PHP PHP如何使用Google API获取完整的经度和纬度 - PHP How to get the full longitude and the latitude using google API 如何使用google map api在php中获取当前的纬度和经度? - How to get current latitude and longitude in php using google map api? 如何使用经度和纬度计算餐厅与该餐厅附近的 10 个司机之间的距离? - How to calculate the distance between a restaurant and 10 drivers who are nearby to that restaurant restaurant using longitude and latitude? 使用php和mysql计算2点之间的距离 - calculate distance between 2 points using php and mysql
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM