[转] 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

时间: 2024-10-19 16:50:03

[转] PHP计算两个坐标之间的距离, Calculate the Distance Between Two Points in PHP的相关文章

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

<?php /** * 计算两点之间的距离 * @param $lng1 经度1 * @param $lat1 纬度1 * @param $lng2 经度2 * @param $lat2 纬度2 * @param int $unit m,km * @param int $decimal 位数 * @return float */ function getDistance($lng1, $lat1, $lng2, $lat2, $unit = 2, $decimal = 2) { $EARTH_R

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

DELIMITER $$CREATE DEFINER = CURRENT_USER FUNCTION `getDistance`(`lon1` float,`lat1` float,`lon2` float,`lat2` float) RETURNS doublebegin declare d double; declare radius int; set radius = 6378140; #假设地球为正球形,直径为6378140米 set d = (2*ATAN2(SQRT(SIN((lat

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

select *,(2 * 6378.137* ASIN(SQRT(POW(SIN(PI()*(111.86141967773438-latitude)/360),2)+COS(PI()*33.07078170776367/180)* COS(latitude * PI()/180)*POW(SIN(PI()*(33.07078170776367-longitude)/360),2)))) as juli from school_base_infoorder by juli asc limit

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

搜索附近地点,例如,坐标(39.91, 116.37)附近500米内的人,首先算出“给定坐标附近500米”这个范围的坐标范围. 虽然它是个圆,但我们可以先求出该圆的外接正方形,然后拿正方形的经纬度范围去搜索数据库.红色部分为要求的搜索范围,绿色部分为实际搜索范围. /** * 获取周围坐标 * Enter description here ... * @param unknown_type $lng 固定坐标经度 * @param unknown_type $lat 固定坐标纬度 * @para

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

//计算两个经纬度之间的距离 /** * 计算两点地理坐标之间的距离 * @param Decimal $longitude1 起点经度 * @param Decimal $latitude1 起点纬度 * @param Decimal $longitude2 终点经度 * @param Decimal $latitude2 终点纬度 * @param Int $unit 单位 1:米 2:公里 * @param Int $decimal 精度 保留小数位数 * @return Decimal

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

一 丶 -(double)distanceBetweenOrderBy:(double) lat1 :(double) lat2 :(double) lng1 :(double) lng2{ CLLocation *curLocation = [[CLLocation alloc] initWithLatitude:lat1 longitude:lng1]; CLLocation *otherLocation = [[CLLocation alloc] initWithLatitude:lat2

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

EARTH_REDIUS = 6378.137 def rad(d): return d * pi / 180.0 def getDistance(lat1, lng1, lat2, lng2): radLat1 = rad(lat1) radLat2 = rad(lat2) a = radLat1 - radLat2 b = rad(lng1) - rad(lng2) s = 2 * math.asin(math.sqrt(math.pow(sin(a/2), 2) + cos(radLat1

百度地图计算两个点之间的距离

BMKMapPoint point1 = BMKMapPointForCoordinate(CLLocationCoordinate2DMake(38.085178, 114.502358)); BMKMapPoint point2 = BMKMapPointForCoordinate(CLLocationCoordinate2DMake(38.085171, 114.502312)); CLLocationDistance distance = BMKMetersBetweenMapPoint

计算两个GPS经纬度坐标之间的距离 PHP

不多说了,自己代码就行,你可以直接拿来用. /** * 计算两个坐标之间的距离(米) * @param float $fP1Lat 起点(纬度) * @param float $fP1Lon 起点(经度) * @param float $fP2Lat 终点(纬度) * @param float $fP2Lon 终点(经度) * @return int */ function distanceBetween($fP1Lat, $fP1Lon, $fP2Lat, $fP2Lon){ $fEARTH_