在dcat-Admin中实现通过下拉选择框搜索地图地址信息流程步骤
2023-06-01 00:00:00
dcat
搜索地图地址信息比如,城市、经纬度等,看看DcatAdmin中下拉选择框效果图:
进入功能开发步骤:
1.添加一个选择框组件
protected function form(): Form
{
return Form::make(new Shop(), function (Form $form) {
$form->select('address', '地址')
->options()
->ajax('map_search', 'dat')
->placeholder('请输入详细地址中的关键字')
->required();
});
}
2.添加路由
//在 Admin/routes.php 文件中添加路由(仅为示例)
//$router->get('map_search','\App\Http\Controllers\Common\[email protected]');
/**
* 根据关键词搜索地址
* @param Request $request
* @return array
*/
public function getAddrByKeyword(Request $request,TXMapService $TXMapService)
{
return $TXMapService->getListByKeyword($request->input('q'), ['page' => $request->input('page')]);
}
3.添加搜索地址方法
<?php
namespace App\Services\Map;
use GuzzleHttp\Client;
class TXMapService
{
private string $key;
public function __construct()
{
//这里用腾讯地图做示例
$this->key = env('TENCENT_MAP_KEY');
}
/**
* 关键词查询地址
*
* @param string $keyword
* @param array $where
* ['region' => '厦门', 'page' => 1, 'limit' => 10]
*
* @return array
*/
public function getListByKeyword($keyword, $where = [])
{
$page = empty($where['page']) ? 0 : (int)$where['page'];
$limit = empty($where['limit']) ? 10 : (int)$where['limit'];
$data = [
'total' => 100,
'per_page' => $limit,
'current_page' => $page == 0 ? 1 : $page,
'last_page' => 100 / $limit,
'next_page_url' => $page + 1,
'from' => ($page == 1 ? 0 : $page) * $limit + 1,
'to' => (($page == 0 ? 1 : $page) + 1) * $limit,
'data' => []
];
if (!empty($keyword)) {
$client = new Client();
$url = 'https://apis.map.qq.com/ws/place/v1/suggestion/?keyword=' . urlencode($keyword) . '&key=' . $this->key;
// 限制城市范围,根据城市名称限制地域范围,空时侧进行全国范围搜索
if (!empty($where['region'])) $url .= '®ion=' . urlencode($where['region']);
// page_index,页码,从1开始,最大页码需通过count进行计算,必须与page_size同时使用
// page_size,每页条数,取值范围1-20,必须与page_index同时使用
$url .= '&page_index=' . $data['current_page'] . '&page_size=' . $limit;
$res = $client->request('GET', $url, [
'headers' => [
'Accept' => 'application/json',
'Accept-Charset' => 'utf-8'
],
'timeout' => 5,
'verify' => false
]);
if ($res->getStatusCode() == 200) {
$res = json_decode($res->getBody()->getContents(), true);
if ($res['status'] == '0') {
$data['total'] = $res['count'];
foreach ($res['data'] as $v) {
$param['lat'] = $v['location']['lat'];
$param['lng'] = $v['location']['lng'];
$param['address'] = $v['address'];
$param['title'] = $v['title'];
$param['adcode'] = $v['adcode'];
$data['data'][] = [
'dat' => json_encode($param,JSON_UNESCAPED_UNICODE),
'text' => $v['address'] . '【' . $v['title'] . '】'
];
}
}
}
}
if (empty($data['data'])) $data['next_page_url'] = null;
return $data;
}
}
4.接下来就是处理你自己的数据 TO DO...
<?php
namespace App\Admin\Repositories;
use App\Models\Shop as Model;
use Dcat\Admin\Form;
use Dcat\Admin\Repositories\EloquentRepository;
class Shop extends EloquentRepository
{
/**
* Model.
*
* @var string
*/
protected $eloquentClass = Model::class;
public function store(Form $form)
{
// 获取待新增的数据
$attributes = $form->updates();
$address = json_decode($attributes['address'],true);
dd($address);
}
}
对laravel-admin感兴趣的可以看看一下功能:
laravel-admin表单提交两级联动功能编写
https://www.zongscan.com/demo333/254.html
laravel-admin中实现三级联动筛选搜索功能流程步骤:
https://www.zongscan.com/demo333/9782.html
相关文章