228 lines
7.9 KiB
PHP
Executable File
228 lines
7.9 KiB
PHP
Executable File
<?php
|
||
// +----------------------------------------------------------------------
|
||
// | ShopXO 国内领先企业级B2C免费开源电商系统
|
||
// +----------------------------------------------------------------------
|
||
// | Copyright (c) 2011~2019 http://shopxo.net All rights reserved.
|
||
// +----------------------------------------------------------------------
|
||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||
// +----------------------------------------------------------------------
|
||
// | Author: Devil
|
||
// +----------------------------------------------------------------------
|
||
namespace base;
|
||
|
||
/**
|
||
* 坐标转换工具
|
||
* @author Devil
|
||
* @blog http://gong.gg/
|
||
* @version 0.0.1
|
||
* @datetime 2018-06-04T21:51:08+0800
|
||
*/
|
||
class GeoTransUtil
|
||
{
|
||
private static $x_pi = 3.14159265358979324*3000.0/180.0;
|
||
private static $pi = 3.14159265358979324;
|
||
private static $a = 6378245.0;
|
||
private static $ee = 0.00669342162296594323;
|
||
private static $earth_radius = 6378.137;
|
||
|
||
/**
|
||
* 计算两组经纬度坐标 之间的距离
|
||
* params :lat1 纬度1; lng1 经度1; lat2 纬度2; lng2 经度2; len_type (1:m or 2:km);
|
||
* return m or km
|
||
*/
|
||
|
||
/**
|
||
* [get_distance 获取两个坐标之间的距离]
|
||
* @author Devil
|
||
* @blog http://gong.gg/
|
||
* @version 1.0.0
|
||
* @datetime 2017-10-23T09:33:58+0800
|
||
* @param [float] $lng1 [经度1]
|
||
* @param [float] $lat1 [维度1]
|
||
* @param [float] $lng2 [经度2]
|
||
* @param [float] $lat2 [维度2]
|
||
* @param [int] $len_type [返回结果类型(1千米, 2公里)默认千米]
|
||
* @param [int] $decimal [保留小数位数(默认2)]
|
||
* @return [int|float] [返回千米或公里值]
|
||
*/
|
||
public static function get_distance($lng1, $lat1, $lng2, $lat2, $len_type = 1, $decimal = 2)
|
||
{
|
||
$radLat1 = $lat1 * self::$pi / 180.0;
|
||
$radLat2 = $lat2 * self::$pi / 180.0;
|
||
$a = $radLat1 - $radLat2;
|
||
$b = ($lng1 * self::$pi / 180.0) - ($lng2 * self::$pi / 180.0);
|
||
$s = 2 * asin(sqrt(pow(sin($a/2),2) + cos($radLat1) * cos($radLat2) * pow(sin($b/2),2)));
|
||
$s = $s * self::$earth_radius;
|
||
if($len_type == 1)
|
||
{
|
||
$s = round($s * 1000);
|
||
}
|
||
return round($s, $decimal);
|
||
}
|
||
|
||
/**
|
||
* [gcj_to_bd 火星坐标(GCJ02坐标,高德,谷歌,腾讯坐标)到百度坐标BD-09]
|
||
* @author Devil
|
||
* @blog http://gong.gg/
|
||
* @version 1.0.0
|
||
* @datetime 2017-10-18T13:58:14+0800
|
||
* @param [float] $lng [经度]
|
||
* @param [float] $lat [纬度]
|
||
* @return [array] [转换后的进维度]
|
||
*/
|
||
public static function gcj_to_bd($lng, $lat)
|
||
{
|
||
$result = ['lng'=>0, 'lat'=>0];
|
||
if(empty($lng) || empty($lat))
|
||
{
|
||
return $result;
|
||
}
|
||
|
||
$x = $lng;
|
||
$y = $lat;
|
||
$z = sqrt($x * $x + $y * $y) + 0.00002 * sin($y * self::$x_pi);
|
||
$theta = atan2($y, $x) + 0.000003 * cos($x * self::$x_pi);
|
||
$result['lng'] = $z * cos($theta) + 0.0065;
|
||
$result['lat'] = $z * sin($theta) + 0.006;
|
||
return $result;
|
||
}
|
||
|
||
/**
|
||
* [bd_to_gcj 百度坐标BD-09到火星坐标GCJ02(高德,谷歌,腾讯坐标)]
|
||
* @author Devil
|
||
* @blog http://gong.gg/
|
||
* @version 1.0.0
|
||
* @datetime 2017-10-18T13:58:14+0800
|
||
* @param [float] $lng [经度]
|
||
* @param [float] $lat [纬度]
|
||
* @return [array] [转换后的进维度]
|
||
*/
|
||