fastadmin 地址转经纬度

 添加地址时并把经纬度存储起来



//根据腾讯地图的key 来进行地址转换
/*$address['province'].$address['city'].$address['district'].$address['street'].$address['detail']  这是前端传来的省市区,详细地址。拼接下来进行解析
$url = "https://apis.map.qq.com/ws/geocoder/v1/?key= ?&address=".urlencode($address['province'].$address['city'].$address['district'].$address['street'].$address['detail']);
                // dump($url);
                 
                $poi = file_get_contents($url);
                $result = json_decode($poi,true);
       if ($result['status']==0) {

             $location = $result['result']['location']['lat'].",".$result['result'] 
                ['location']['lng'];
          }else{
                    $this->error("地址解析出错,请联系管理员!错误码:".$result['status']);
            }
            //把经纬度存储到 $address['location'] 这个字段中
            $address['location'] = $location;

全部的代码:

 /**
     * 添加
     *
     * @return string
     * @throws \think\Exception
     */
    public function add()
    {
        if (false === $this->request->isPost()) {
            return $this->view->fetch();
        }
        $params = $this->request->post('row/a');

        if (empty($params)) {
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $params = $this->preExcludeFields($params);

        if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
            $params[$this->dataLimitField] = $this->auth->id;
        }
        $url = "https://apis.map.qq.com/ws/geocoder/v1/?key=4JNBZ-KTMCW-5BZR7-YPPYU-FSPOJBQFML&address=".urlencode($params['province'].$params['city'].$params['district'].$params['address']);
        $poi = file_get_contents($url);
        $res = json_decode($poi,true);
        if ($res['status']==0) {
            $location = $res['result']['location']['lat'].",".$res['result']['location']['lng'];

        }else{
            $this->error("地址解析出错,请联系管理员!错误码:".$res['status']);
        }
        $params['location'] = $location;
        $result = false;
        Db::startTrans();
        try {
            //是否采用模型验证
            if ($this->modelValidate) {
                $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
                $this->model->validateFailException()->validate($validate);
            }
            $result = $this->model->allowField(true)->save($params);
            Db::commit();
        } catch (ValidateException|PDOException|Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        if ($result === false) {
            $this->error(__('No rows were inserted'));
        }
        $this->success();
    }

同时,修改同理

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值