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_RADIUS = 6370.996; // 地球半径系数
    $PI           = 3.1415926535898;

    $radLat1 = $lat1 * $PI / 180.0;
    $radLat2 = $lat2 * $PI / 180.0;

    $radLng1 = $lng1 * $PI / 180.0;
    $radLng2 = $lng2 * $PI / 180.0;

    $a = $radLat1 - $radLat2;
    $b = $radLng1 - $radLng2;

    $distance = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2)));
    $distance = $distance * $EARTH_RADIUS * 1000;

    if ($unit === 2) {
        $distance /= 1000;
    }

    return round($distance, $decimal);
}

// 起点坐标
$lng1 = 118.34593200683594;
$lat1 = 33.9527587890625;

// 终点坐标
$lng2 = 118.301612;
$lat2 = 33.936781;

$distance = getDistance($lng1, $lat1, $lng2, $lat2, 1);
echo $distance . 'm' . PHP_EOL; // 4457.64m

$distance = getDistance($lng1, $lat1, $lng2, $lat2);
echo $distance . 'km' . PHP_EOL; // 4.46km

原文地址:https://www.cnblogs.com/jiqing9006/p/11404425.html

时间: 2024-08-10 23:27:15

PHP计算两个坐标之间的距离的相关文章

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

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_