cxfoot/modules/api/controllers/GeocoderController.php
2023-10-24 14:54:18 +08:00

123 lines
5.8 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* @author Any
* @description KISS
* @date 2021年6月2日
* @version 1.0.0
*
* _____LOG_____
*
*/
namespace app\modules\api\controllers;
use app\modules\api\behaviors\LoginBehavior;
use app\models\District;
use app\components\Serializer;
use Geocoder\Geocoder;
use app\modules\api\models\GeocoderForm;
class GeocoderController extends Controller
{
public function behaviors() {
return array_merge(parent::behaviors(),[
'login' => [
'class' => LoginBehavior::className(),
'ignore' => [
'api/geocoder/district',
'api/geocoder/district-info',
'api/geocoder/location',
]
]
]);
}
/**
* showdoc
* @catalog 地理相关/国内数据
* @title 省份城市数据
* @description 本接口提供国内行政区域规划数据
* @method get
* @url /api/geocoder/district
* @param type 必选 int type为0时显示全国行政区域按省份分类树状type=1时显示字母索引城市列表默认为0
* @return {"code":0,"msg":"ok","data":"[]"}
* @return_param id int 省、市、区、县ID
* @return_param name string 名称
* @return_param parent_id int 上级行政区ID
* @return_param level string 层级province、city、district
* @return_param list array 下级行政区
* @remark
*/
public function actionDistrict()
{
$type = \Yii::$app->request->get('type',0);
if($type == 0){
$district = District::getTerritorial();
$district= (new Serializer())->encode($district);
} else if($type == 1) {
$district = District::getCityIndexList(['city']);
$district= (new Serializer())->encode($district);
}
$data = [
'code' => 0,
'msg' => 'ok',
'data' => $district
];
return $this->responseHandler($data);
}
/**
* showdoc
* @catalog 地理相关/地址解析
* @title 逆地址解析
* @description 本接口提供由坐标到坐标所在位置的文字描述的转换。输入坐标返回地理位置信息
* @method get
* @url /api/geocoder/location
* @param lat 必选 string 纬度
* @param lng 必选 string 经度
* @return {"code":0,"msg":"ok","data":{"location":{"lat":39.984154,"lng":116.30749},"address":"北京市海淀区北四环西路66号","formatted_addresses":{"recommend":"海淀区中关村中国技术交易大厦","rough":"海淀区中关村中国技术交易大厦"},"address_component":{"nation":"中国","province":"北京市","city":"北京市","district":"海淀区","street":"北四环西路","street_number":"北四环西路66号"},"ad_info":{"nation_code":"156","adcode":"110108","city_code":"156110000","name":"中国,北京市,北京市,海淀区","location":{"lat":40.045132,"lng":116.375},"nation":"中国","province":"北京市","city":"北京市","district":"海淀区"},"address_reference":{"business_area":{"id":"14178584199053362783","title":"中关村","location":{"lat":39.980598,"lng":116.310997},"_distance":0,"_dir_desc":"内"},"famous_area":{"id":"14178584199053362783","title":"中关村","location":{"lat":39.980598,"lng":116.310997},"_distance":0,"_dir_desc":"内"},"crossroad":{"id":"529961","title":"彩和坊路/海淀北一街(路口)","location":{"lat":39.983952,"lng":116.308228},"_distance":61.5,"_dir_desc":"西"},"town":{"id":"110108012","title":"海淀街道","location":{"lat":39.974819,"lng":116.284409},"_distance":0,"_dir_desc":"内"},"street_number":{"id":"595672509379194165901290","title":"北四环西路66号","location":{"lat":39.984089,"lng":116.308037},"_distance":45.8,"_dir_desc":""},"street":{"id":"","title":"北四环西路","location":{"lat":0,"lng":0},"_distance":103.7,"_dir_desc":"南"},"landmark_l2":{"id":"3629720141162880123","title":"中国技术交易大厦","location":{"lat":39.984104,"lng":116.307503},"_distance":0,"_dir_desc":"内"}}}}
* @return_param address string 地址描述
* @remark 经纬度为gcj02坐标
*/
public function actionLocation()
{
if(!\Yii::$app->request->isGet){
$data = $this->invaildRequest();
return $this->responseHandler($data);
}
$lat = \Yii::$app->request->get('lat');
$lng = \Yii::$app->request->get('lng');
$provider = "qqmap";
$key = \Yii::$app->params['mapKey'][$provider];
$geocoder = new Geocoder($key,$provider);
$data = $geocoder->getAddress($lat, $lng);
return $this->responseHandler($data);
}
/**
* showdoc
* @catalog 地理相关/地址解析
* @title 逆地址解析获取行政区域
* @description 本接口提供由坐标到坐标所在位置的文字描述的转换。输入坐标返回行政区域ID信息
* @method get
* @url /api/geocoder/district-info
* @param lat 必选 string 纬度
* @param lng 必选 string 经度
* @return {"code":0,"msg":"ok","data":{"location":{"lat":39.984154,"lng":116.30749},"address":"北京市海淀区北四环西路66号","province":{"id":"1","name":"北京市"},"city":{"id":"32","name":"北京市"},"district":{"id":"376","name":"海淀区"},"street":{"name":"北四环西路","street_number":"北四环西路66号"},"formatted_addr":"海淀区中关村中国技术交易大厦(彩和坊路)"}}
* @return_param address string 地址描述
* @remark 经纬度为gcj02坐标
*/
public function actionDistrictInfo()
{
if(!\Yii::$app->request->isGet){
$data = $this->invaildRequest();
return $this->responseHandler($data);
}
$form = new GeocoderForm();
$form->attributes = \Yii::$app->request->get();
$data = $form->get_district_info();
return $this->responseHandler($data);
}
}