[转] PHP计算两个坐标之间的距离, Calculate the Distance Between Two Points in PHP
Calculate the Distance Between Two Points in PHP
There are a lot of applications where it is useful to know the distance between two coordinates. Here, you'll find a PHP function that takes the latitude and longitude of two points and returns the distance between them in both miles and metric units.
You can also use this to find the distance between two addresses by taking advantage of the Google Geotargetting API.
Here's the function:
function get_distance_between_points($latitude1, $longitude1, $latitude2, $longitude2) {
$theta = $longitude1 - $longitude2;
$miles = (sin(deg2rad($latitude1)) * sin(deg2rad($latitude2))) + (cos(deg2rad($latitude1)) * cos(deg2rad($latitude2)) * cos(deg2rad($theta)));
$miles = acos($miles);
$miles = rad2deg($miles);
$miles = $miles * 60 * 1.1515;
$feet = $miles * 5280;
$yards = $feet / 3;
$kilometers = $miles * 1.609344;
$meters = $kilometers * 1000;
return compact('miles','feet','yards','kilometers','meters');
}
调用
And here's an example of the function in action, using two coordinates in New York City:
$point1 = array('lat' => 40.770623, 'long' => -73.964367);
$point2 = array('lat' => 40.758224, 'long' => -73.917404);
$distance = get_distance_between_points($point1['lat'], $point1['long'], $point2['lat'], $point2['long']);
foreach ($distance as $unit => $value) { echo $unit.': '.number_format($value,4).'<br />'; }
The example returns the following:
miles: 2.6025 //英里
feet: 13,741.4350 //英尺
yards: 4,580.4783 //码
kilometers: 4.1884 //公里(km)
meters: 4,188.3894 //米(m)
FROM : https://inkplant.com/code/calculate-the-distance-between-two-points.php
[转] PHP计算两个坐标之间的距离, Calculate the Distance Between Two Points in PHP的更多相关文章
- PHP计算两个坐标之间的距离
<?php /** * 计算两点之间的距离 * @param $lng1 经度1 * @param $lat1 纬度1 * @param $lng2 经度2 * @param $lat2 纬度2 ...
- iOS 计算两个坐标之间的距离
//第一个坐标 CLLocation *before=[[CLLocation alloc] initWithLatitude:29.553968 longitude:106.538872]; //第 ...
- mysql实现经纬度计算两个坐标之间的距离sql语句
select *,(2 * 6378.137* ASIN(SQRT(POW(SIN(PI()*(111.86141967773438-latitude)/360),2)+COS(PI()*33.070 ...
- mysql实现经纬度计算两个坐标之间的距离
DELIMITER $$CREATE DEFINER = CURRENT_USER FUNCTION `getDistance`(`lon1` float,`lat1` float,`lon2` fl ...
- PHP MYSQL 搜索周边坐标,并计算两个点之间的距离
搜索附近地点,例如,坐标(39.91, 116.37)附近500米内的人,首先算出“给定坐标附近500米”这个范围的坐标范围. 虽然它是个圆,但我们可以先求出该圆的外接正方形,然后拿正方形的经纬度范围 ...
- IOS 计算两个经纬度之间的距离
IOS 计算两个经纬度之间的距离 一 丶 -(double)distanceBetweenOrderBy:(double) lat1 :(double) lat2 :(double) lng1 :(d ...
- 高德地图 API 计算两个城市之间的距离
1. 目前在项目中,遇到一个需求不会做,就是要计算两个城市之间的距离,而这两个城市的输入是可变的,如果要使用数据库来先存储两地之间的距离,调用的时候再来调用,那么存数据的时候,要哭的,因为光是省级区域 ...
- 计算两个坐标点的距离(高德or百度)
/// <summary> /// 获取两个坐标之间的距离 /// </summary> /// <param name="lat1">第一个坐 ...
- 计算两个经纬度之间的距离(python算法)
EARTH_REDIUS = 6378.137 def rad(d): return d * pi / 180.0 def getDistance(lat1, lng1, lat2, lng2): r ...
随机推荐
- UVA 11039 - Building designing(DP)
题目链接 本质上是DP,但是俩变量就搞定了. #include <cstdio> #include <cstring> #include <algorithm> u ...
- springmvc入门基础之注解和参数传递
一.SpringMVC注解入门 1. 创建web项目2. 在springmvc的配置文件中指定注解驱动,配置扫描器 <!-- mvc的注解驱动 --> <mvc:annotation ...
- mac 终端乱码
我系统是英文的 export LANG=en_US.UTF-8export LC_ALL=en_US.UTF-8 中文的 export LANG=zh_CN.UTF-8 export LANG=en_ ...
- Linux下memcached安装和启动方法
Linux下memcached安装和启动方法 1. 首先下载memcached 和 libevent 包. Memcached用到了libevent这个库用于Socket的处理.下面是下载的两个包文件 ...
- Equal Sum Sets
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=49406 题意: 输入n,k,s,求在不小于n的数中找出k个不同的数 ...
- BFS 骑士的移动
骑士的移动 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=83498#problem/E 题目: Description A f ...
- 一个网页抓取的类支持get+post+cookie存储
前段时间提取了一个工具类,分享给大家: <?php class httpconnector { private $curl; private $cookie; private $kv; func ...
- 基于canvas的前端图片压缩
/*common*/ /** * canvas图片压缩 * @param {[Object]} opt [配置参数] * @param {[Function]} cbk [回调函数] * @retur ...
- cursor 手型样式
cursor:hand 与 cursor:pointer 的效果是一样,都像手形光标.但用FireFox浏览时才注意到使用cursor:hand在FireFox里并被支持.cursor:hand :I ...
- Date类型-演示JS中的日期
<script type="text/javascript"> /* *演示JS中的日期 */ var date = new Date(); document.writ ...