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的更多相关文章

  1. PHP计算两个坐标之间的距离

    <?php /** * 计算两点之间的距离 * @param $lng1 经度1 * @param $lat1 纬度1 * @param $lng2 经度2 * @param $lat2 纬度2 ...

  2. iOS 计算两个坐标之间的距离

    //第一个坐标 CLLocation *before=[[CLLocation alloc] initWithLatitude:29.553968 longitude:106.538872]; //第 ...

  3. mysql实现经纬度计算两个坐标之间的距离sql语句

    select *,(2 * 6378.137* ASIN(SQRT(POW(SIN(PI()*(111.86141967773438-latitude)/360),2)+COS(PI()*33.070 ...

  4. mysql实现经纬度计算两个坐标之间的距离

    DELIMITER $$CREATE DEFINER = CURRENT_USER FUNCTION `getDistance`(`lon1` float,`lat1` float,`lon2` fl ...

  5. PHP MYSQL 搜索周边坐标,并计算两个点之间的距离

    搜索附近地点,例如,坐标(39.91, 116.37)附近500米内的人,首先算出“给定坐标附近500米”这个范围的坐标范围. 虽然它是个圆,但我们可以先求出该圆的外接正方形,然后拿正方形的经纬度范围 ...

  6. IOS 计算两个经纬度之间的距离

    IOS 计算两个经纬度之间的距离 一 丶 -(double)distanceBetweenOrderBy:(double) lat1 :(double) lat2 :(double) lng1 :(d ...

  7. 高德地图 API 计算两个城市之间的距离

    1. 目前在项目中,遇到一个需求不会做,就是要计算两个城市之间的距离,而这两个城市的输入是可变的,如果要使用数据库来先存储两地之间的距离,调用的时候再来调用,那么存数据的时候,要哭的,因为光是省级区域 ...

  8. 计算两个坐标点的距离(高德or百度)

    /// <summary> /// 获取两个坐标之间的距离 /// </summary> /// <param name="lat1">第一个坐 ...

  9. 计算两个经纬度之间的距离(python算法)

    EARTH_REDIUS = 6378.137 def rad(d): return d * pi / 180.0 def getDistance(lat1, lng1, lat2, lng2): r ...

随机推荐

  1. NOIp 2012 #1 Vigenère 密码 Label:模拟

    题目描述 16 世纪法国外交家 Blaise de Vigenère 设计了一种多表密码加密算法――Vigenère 密 码.Vigenère 密码的加密解密算法简单易用,且破译难度比较高,曾在美国南 ...

  2. NOIp 2014 #5 解方程 Label:数论?

    题目描述 已知多项式方程: a0+a1x+a2x^2+..+anx^n=0 求这个方程在[1, m ] 内的整数解(n 和m 均为正整数) 输入输出格式 输入格式: 输入文件名为equation .i ...

  3. POJ-A Simple Problem with Integers

    Description 给出了一个序列,你需要处理如下两种询问. "C a b c"表示给[a, b]区间中的值全部增加c (-10000 ≤ c ≤ 10000). " ...

  4. flex lineChart 显示所有的数据节点

    .If you're using <mx:LineSeries>, then set the following property:itemRenderer="mx.charts ...

  5. Crossing River

    Crossing River 题目链接:http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=26251 题意: N个人希望去过 ...

  6. 图片懒加载插件lazyload使用方法

    图片懒加载插件lazyload使用方法 一.如何使用: Lazy Load 依赖于 jQuery.引入文件 <script type="text/javascript" sr ...

  7. Python 基础 - 随机列表最大的两个值

    # -*- coding: utf-8 -*- #author:v def sywmemeda(l): #list 冒泡排序 length = len(l) for i in range(length ...

  8. 解决微信浏览器无法使用window.location.reload()刷新页面

    解决方法: 使用    window.location.href=window.location.href+随机数    代替 window.location.reload(). function r ...

  9. JS读写cookie以及中文乱码解决

    本文地址:http://www.cnblogs.com/PiaoMiaoGongZi/p/4092489.html 转载请注明. Js获取所有的cookie信息: var cookiename = d ...

  10. Total Commander 常用快捷键(并附快捷键大全)

    Total Commander 常用快捷键 喜欢用Total Commander的人,都会记住它的一些快捷键,这会给你的操作带来很大的方便,以下是经常会用到的快捷键,大家可以记住一些自己用得最多的操作 ...