SoFunction
Updated on 2025-03-10

7 sets of latitude and distance calculation functions implemented by PHP demo

1. Calculate the latitude and longitude of the surroundings based on the current location

/** Calculate the latitude and longitude of the four sides based on the current position
  * @param $lng
  * @param $lat
  * @param float $distance
  * @return array
  */
function returnSquarePoint($lng, $lat, $distance = 0.5)
{
    $earthRadius = 6378138;
    $dlng = 2 * asin(sin($distance / (2 * $earthRadius)) / cos(deg2rad($lat)));
    $dlng = rad2deg($dlng);
    $dlat = $distance / $earthRadius;
    $dlat = rad2deg($dlat);
    return array(
        'left-top' => array('lat' => $lat + $dlat, 'lng' => $lng - $dlng),
        'right-top' => array('lat' => $lat + $dlat, 'lng' => $lng + $dlng),
        'left-bottom' => array('lat' => $lat - $dlat, 'lng' => $lng - $dlng),
        'right-bottom' => array('lat' => $lat - $dlat, 'lng' => $lng + $dlng)
    );
}

2. Calculate the range based on latitude and longitude

/** Calculate the range according to latitude and longitude
  * @param $lat1
  * @param $lng1
  * @param $lat2
  * @param $lng2
  * @return float
  */
function getDistance($lat1, $lng1, $lat2, $lng2)
{
    $earthRadius = 6378138;
    // Approximate Earth's radius meters // Convert to radians    $lat1 = ($lat1 * pi()) / 180;
    $lng1 = ($lng1 * pi()) / 180;
    $lat2 = ($lat2 * pi()) / 180;
    $lng2 = ($lng2 * pi()) / 180;           // Use the semi-right vector formula to calculate with ruler    $calcLongitude = $lng2 - $lng1;
    $calcLatitude = $lat2 - $lat1;
    $stepOne = pow(sin($calcLatitude / 2), 2) + cos($lat1) * cos($lat2) * pow(sin($calcLongitude / 2), 2);
    $stepTwo = 2 * asin(min(1, sqrt($stepOne)));
    $calculatedDistance = $earthRadius * $stepTwo;
    return round($calculatedDistance);
}

3. Order from near to far according to distance by latitude and longitude

/** Sort by latitude and longitude from near to far according to distance
  * @param $lat
  * @param $lng
  * @return mixed
  */
function computePoint($lat, $lng)
{
    $page = 1;
    $pageSize = 7;
    $EARTH = 6378.137;         // Fixed parameters Earth radius    $PI = 3.1415926535898;  // Fixed parameters Pi    $list = db('work')->alias('wk')
        ->field("
        ,wk.work_name,,,,wk.start_money,wk.work_description,wk.xz_id,
        ,cp.company_id,cp.company_name,,,
        (2 * $EARTH* ASIN(SQRT(POW(SIN($PI*(" . $lat . "-)/360),2)+COS($PI*" . $lat . "/180)* COS( * $PI/180)*POW(SIN($PI*(" . $lng . "-)/360),2)))) as juli
        ")
        ->order('create_time desc,juli asc')
        ->page($page, $pageSize)
        ->select()->toArray();
    return $list;
}

4. Query the geographical location based on latitude and longitude

/** Query the geographical location based on latitude and longitude
  * @param $lat
  * @param $lng
  */
function myLocation($lat, $lng)
{
    $url = "/geocoder/v2/?ak=YQH8OyfGcvOsPlHdnssSpkulaSNVgL0N&callback=renderReverse&location=$lat,$lng&output=json&pois=1";
    $res = file_get_contents($url);
    $lres = ltrim($res, "renderReverse && renderReverse(");
    $rres = rtrim($lres, ")");
    echo $rres;
}

5. Calculate the straight line distance based on the latitude and longitude

/**
  * Calculate the straight line distance based on latitude and longitude
  * @param $lat1
  * @param $lng1
  * @param $lat2
  * @param $lng2
  * @return float|int
  */
function getDistances($lat1, $lng1, $lat2, $lng2)
{
    define('PI', 3.1415926535898);
    define('EARTH_RADIUS', 6378.137);
    $radLat1 = $lat1 * (PI / 180);
    $radLat2 = $lat2 * (PI / 180);
    $a = $radLat1 - $radLat2;
    $b = ($lng1 * (PI / 180)) - ($lng2 * (PI / 180));
    $s = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2)));
    $s = $s * EARTH_RADIUS;
    $s = round($s * 10000) / 10000;
    return $s * 1000;
}

6. Calculate the range based on latitude and longitude and radius

/** Calculate the range based on latitude and longitude and radius
  * @param string $lat
  * @param String $lng Longitude
  * @param float $radius
  * @return Array range array
  */
function calcScope($lat, $lng, $radius)
{
    $degree = (24901 * 1609) / 360.0;
    $dpmLat = 1 / $degree;
    $radiusLat = $dpmLat * $radius;
    $minLat = $lat - $radiusLat;       // Minimum latitude    $maxLat = $lat + $radiusLat;       // Maximum latitude    $mpdLng = $degree * cos($lat * (3.141592 / 180));
    $dpmLng = 1 / $mpdLng;
    $radiusLng = $dpmLng * $radius;
    $minLng = $lng - $radiusLng;      // Minimum longitude    $maxLng = $lng + $radiusLng;      // Maximum longitude    /** Return range array */
    $scope = array(
        'minLat' => $minLat,
        'maxLat' => $maxLat,
        'minLng' => $minLng,
        'maxLng' => $maxLng
    );
    return $scope;
}

7. Obtain the distance between two latitudes and longitudes

/** Get the distance between two latitudes and longitudes
  * @param string $lat1
  * @param String $lng1
  * @param String $lat2
  * @param String $lng2
  * @return float Returns the distance between two points
  */
function calcDistance($lat1, $lng1, $lat2, $lng2)
{
    if (empty($lat1) || empty($lng1) || empty($lat2) || empty($lng2)) {
        return false;
    }
    /** Convert data type to double */
    $lat1 = doubleval($lat1);
    $lng1 = doubleval($lng1);
    $lat2 = doubleval($lat2);
    $lng2 = doubleval($lng2);
    $theta = $lng1 - $lng2;
    $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;
    return ($miles * 1.609344);
}

The above is the detailed content of the 7 sets of latitude and longitude calculation functions implemented by PHP. For more information about PHP latitude and longitude distance calculation functions, please pay attention to my other related articles!