添加地址时并把经纬度存储起来
//根据腾讯地图的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();
}
同时,修改同理