DcatAdmin 透過下拉選擇框搜尋地圖地址(經緯度,城市等資訊)

JefChen發表於2022-11-24

DcatAdmin 透過下拉選擇框搜尋地圖地址(經緯度,城市等資訊)

效果展示如下

DcatAdmin 透過下拉選擇框搜尋地圖地址(經緯度,城市等資訊)

DcatAdmin 透過下拉選擇框搜尋地圖地址(經緯度,城市等資訊)

新增一個選擇框元件

protected function form(): Form
{
    return Form::make(new Shop(), function (Form $form) {
        $form->select('address', '地址')
        ->options()
        ->ajax('map_search', 'dat')
        ->placeholder('請輸入詳細地址中的關鍵字')
        ->required();
    });
}

//在 Admin/routes.php 檔案中新增路由(僅為示例)
//$router->get('map_search','\App\Http\Controllers\Common\MapController@getAddrByKeyword');
/**
* 根據關鍵詞搜尋地址
* @param Request $request
* @return array
*/
public function getAddrByKeyword(Request $request,TXMapService $TXMapService)
{
    return $TXMapService->getListByKeyword($request->input('q'), ['page' => $request->input('page')]);
}

搜尋地址方法如下

<?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 .= '&region=' . 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;
    }
}

接下來只要處理你自己的資料就可以了

<?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);
    }
}
本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章