📖 File Reader
<?php
namespace app\common;
use app\BaseController;
/*地图相关处理*/
class Common_map extends BaseController
{
const x_PI = 52.35987755982988;
const PI = 3.1415926535897932384626;
const a = 6378245.0;
const ee = 0.00669342162296594323;
/**
* 大地坐标系转高德经纬度
* @param $lng 经度
* @param $lat 维度
* @return array
*/
public function wgs84togcj02($lng, $lat) {
if (empty($lng) || empty($lat)){
return $this->jsonError('缺少参数');
}
$dlat = $this->transformlat($lng - 105.0, $lat - 35.0);
$dlng = $this->transformlng($lng - 105.0, $lat - 35.0);
$radlat = $lat / 180.0 * self::PI;
$magic = sin($radlat);
$magic = 1 - self::ee * $magic * $magic;
$sqrtmagic = sqrt($magic);
$dlat = ($dlat * 180.0) / ((self::a * (1 - self::ee)) / ($magic * $sqrtmagic) * self::PI);
$dlng = ($dlng * 180.0) / (self::a / $sqrtmagic * cos($radlat) * self::PI);
$mglat = $lat + $dlat;
$mglng = $lng + $dlng;
$data = [
'mylng' => $mglng,
'mylat' => $mglat,
];
return $data;
}
private function transformlat($lng, $lat) {
$ret = -100.0 + 2.0 * $lng + 3.0 * $lat + 0.2 * $lat * $lat + 0.1 * $lng * $lat + 0.2 * sqrt(abs($lng));
$ret += (20.0 * sin(6.0 * $lng * self::PI) + 20.0 * sin(2.0 * $lng * self::PI)) * 2.0 / 3.0;
$ret += (20.0 * sin($lat * self::PI) + 40.0 * sin($lat / 3.0 * self::PI)) * 2.0 / 3.0;
$ret += (160.0 * sin($lat / 12.0 * self::PI) + 320 * sin($lat * self::PI / 30.0)) * 2.0 / 3.0;
return $ret;
}
private function transformlng($lng, $lat) {
$ret = 300.0 + $lng + 2.0 * $lat + 0.1 * $lng * $lng + 0.1 * $lng * $lat + 0.1 * sqrt(abs($lng));
$ret += (20.0 * sin(6.0 * $lng * self::PI) + 20.0 * sin(2.0 * $lng * self::PI)) * 2.0 / 3.0;
$ret += (20.0 * sin($lng * self::PI) + 40.0 * sin($lng / 3.0 * self::PI)) * 2.0 / 3.0;
$ret += (150.0 * sin($lng / 12.0 * self::PI) + 300.0 * sin($lng / 30.0 * self::PI)) * 2.0 / 3.0;
return $ret;
}
/**
* 坐标转换-转为高德坐标(key需要用web服务 不要用web端(jsapi))
* @param $longitude 经度
* @param $latitude 纬度
* @return void
*/
public function coordinate_rotation($longitude='',$latitude='')
{
$key = 'b5994ece8096c93afa0f1210adb8fce9';
$locations = $longitude.','.$latitude;
$url = "restapi.amap.com/v3/assistant/coordinate/convert?key={$key}&locations={$locations}&coordsys=gps";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$response_data = json_decode($response, true);
if ($response_data["status"] == 1){
$locations_arr = explode(',',$response_data['locations']);
$data = [
'msg'=>$response_data["info"],
'code'=>200,
'longitude'=>$locations_arr[0],
'latitude'=>$locations_arr[1],
];
return $data;
}else{
$data = [
'msg'=>$response_data["info"],
'code'=>400,
'locations'=>$response_data["locations"],
];
return $data;
}
}
/**
* 高德根据地址获取经纬度(key需要用web服务 不要用web端(jsapi))
* @param $address 地址名称:黑龙江省哈尔滨市南岗区禧龙大市场
* @return mixed
*/
public function addresstolatlag($address){
$key="0ae98b23ab8c8fdc5775758c25e8a1e9";
$regeo_url="https://restapi.amap.com/v3/geocode/geo";
$address_location=$regeo_url."?output=JSON&address=$address&key=$key";
$data_location=file_get_contents($address_location);
$arr_return=[];
$result_local=json_decode($data_location,true);
if($result_local['status'] == 1 && $result_local['infocode']== 10000){
$location=$result_local['geocodes'][0]['location'];
$arr=explode(',',$location);
$arr_return['lng']=$arr[0];//经度
$arr_return['lat']=$arr[1];//纬度
}
return $arr_return;
}
/**
* 高德根据经纬度获取地址(key需要用web服务 不要用web端(jsapi))
* @param $address 126.683135,45.752830
* @return mixed
*/
public function getaddress($address)
{
$url = "http://restapi.amap.com/v3/geocode/regeo?output=json&location=" . $address . "&key=0ae98b23ab8c8fdc5775758c25e8a1e9";
if ($result = file_get_contents($url)) {
$result = json_decode($result, true);
if (!empty($result['status']) && $result['status'] == 1) {
$data = [
'province'=>$result['regeocode']['addressComponent']['province'],
'city'=>$result['regeocode']['addressComponent']['city'],
'district'=>$result['regeocode']['addressComponent']['district'],
'formatted_address'=>$result['regeocode']['formatted_address'],
];
return $data;
} else {
return false;
}
}
}
/**
* 计算距离
* @param Decimal $lat1 起点纬度
* @param Decimal $lng1 起点经度
* @param Decimal $lat2 终点纬度
* @param Decimal $lng2 终点经度
* @param string $is_mi 统一单位米:1是 0否
*/
public function getDistance($lat1, $lng1, $lat2, $lng2,$is_mi=0){
$earthRadius = 6367000; //近似地球半径(米)
$lat1 = ($lat1 * pi() ) / 180;
$lng1 = ($lng1 * pi() ) / 180;
$lat2 = ($lat2 * pi() ) / 180;
$lng2 = ($lng2 * 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)));
$Distance = $earthRadius * $stepTwo;
if ($is_mi == 0){
if ($Distance >= 1000) {
$Distance = round($Distance/1000,2);
return $Distance . 'km';
}
return round($Distance) . 'm';
}else{
return round($Distance);
}
}
/**
* 高德根据起点终点经纬度规划路线(key需要用web服务 不要用web端(jsapi))
* @param string $origin 起点坐标经度纬度(126.68307500875184,45.75324689479813)
* @param string $destination 终点坐标经度纬度(126.63125610351562,45.760921478271484)
* @return void
*/
public function get_directions($origin='',$destination='')
{
$key = 'b5994ece8096c93afa0f1210adb8fce9';
$url = "https://restapi.amap.com/v3/direction/driving?key={$key}&origin={$origin}&destination={$destination}";
// 初始化cURL会话
$ch = curl_init();
// 设置cURL选项
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// 执行cURL会话
$response = curl_exec($ch);
// 关闭cURL会话
curl_close($ch);
// 解析JSON响应
$response_data = json_decode($response, true);
if ($response_data["status"] == 1) {
$data = [
'msg'=>$response_data["info"],
'code'=>200,
'route'=>$response_data["route"],
];
return $data;
}else{
$data = [
'msg'=>$response_data["info"],
'code'=>400,
];
return $data;
}
}
}