123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- <?php
- namespace JinDouYun\Model\Common;
- /**
- * @copyright Copyright (c) https://www.qianniaovip.com All rights reserved
- * Description:
- * Class Location
- * @package JinDouYun\Model\Common
- */
- class Location
- {
- public function __construct()
- {
- $this->_geo_conf = array(
- 'pi' => pi(),
- 'EARTH_RADIUS' => 6378.137,
- );
- }
- /**
- * 求两个地理位置之间的距离
- *
- * @param string $latitude_a a坐标纬度
- * @param string $longitude_a a坐标经度
- * @param $latitude_b b坐标纬度
- * @param $longitude_b b坐标经度
- * @return bool|float|int
- */
- public function get_distance_by_geo($latitude_a, $longitude_a, $latitude_b, $longitude_b)
- {
- if (!$longitude_a || !$latitude_a || !$longitude_a || !$longitude_b) {
- return false;
- }
- $earthRadius = 6367000; //approximate radius of earth in meters
- $lat1 = ($latitude_a * pi() ) / 180;
- $lng1 = ($longitude_a * pi() ) / 180;
- $lat2 = ($latitude_b * pi() ) / 180;
- $lng2 = ($longitude_b * pi() ) / 180;
- $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);
- /*$latitude_a_radian = $this->_angle_to_radian($latitude_a);
- $latitude_b_radian = $this->_angle_to_radian($latitude_b);
- $longitude_a_radian = $this->_angle_to_radian($longitude_a);
- $longitude_b_radian = $this->_angle_to_radian($longitude_b);
- $latitude_radian_diff = $latitude_a_radian - $latitude_b_radian;
- $longitude_radian_diff = $longitude_a_radian - $longitude_b_radian;
- $S = 2 * asin(sqrt(pow(sin($latitude_radian_diff / 2), 2) + cos($latitude_a_radian) * cos($latitude_b_radian) * pow(sin($longitude_radian_diff / 2), 2))) * $this->_geo_conf['EARTH_RADIUS'];
- return $S;*/
- }
- /**
- * Doc: (des="")
- * User: XMing
- * Date: 2020/12/10
- * Time: 2:30 下午
- * @param $value
- * @return string
- */
- public static function toMeters($value): string
- {
- if ($value > 1000) {
- return sprintf("%.2f", $value / 1000) . 'km';
- }
- return $value.'m';
- }
- /**
- * 根据某个经纬度和距离范围,求范围内到经纬度最大最小值
- * @param $latitude
- * @param $longitude
- * @param $distance
- * @return array|bool
- */
- public function get_geo_by_distance($latitude, $longitude, $distance)
- {
- if (!$latitude || !$longitude) {
- return false;
- }
- $lat_a = $this->_angle_to_radian($latitude);
- $lng_D = asin(sin($distance / (2 * $this->_geo_conf['EARTH_RADIUS'])) / cos($lat_a)) * 2;
- $lat_D = asin(sin($distance / (2 * $this->_geo_conf['EARTH_RADIUS']))) * 2;
- $lat_D = $this->_radian_to_angle($lat_D);
- $lon_D = $this->_radian_to_angle($lng_D);
- $lat_res = array('lat_min' => ($latitude - $lat_D), 'lat_max' => ($latitude + $lat_D));
- $lon_res = array('lon_min' => ($longitude - $lon_D), 'lon_max' => ($longitude + $lon_D));
- return array($lat_res, $lon_res);
- }
- /**
- * 角度转弧度
- * @param $angle
- * @return float
- */
- private function _angle_to_radian($angle)
- {
- return $angle * $this->_geo_conf['pi'] / 180.0;
- }
- /**
- * 弧度转角度
- * @param $radian
- * @return float|int
- */
- private function _radian_to_angle($radian)
- {
- return abs($radian * 180 / $this->_geo_conf['pi']);
- }
- }
|