好用的地址转径纬度
当我们在结算两者之间距离时
地址转径纬度非常头疼
1.根据经纬度取得地址:
请求地址:
http://api.map.baidu.com/geocoder/v2/?callback=renderReverse&location=30.68093376455154,104.06552381979525&output=json&pois=1&ak=你的ak
在此之前
框架中获取到经纬度的方法
这里我用Laravel 5.8框架举例
Guzzle-http用于发起HTTP请求
Guzzle-http链接: https://guzzle-cn.readthedocs.io/
1、首选在Laravel中安装Guzzle-http插件,安装命令↓↓↓↓↓↓
composer require guzzlehttp/guzzle
使用guzzle来发起GET请求
引入类
// 发起HTTP请求
use GuzzleHttp\Client;
2、在laravel5.8框架中,在config目录下定义,gaode.php 配置文件,代码如下。
return [
// 把地址转为经纬度 %s 占位符
'geocode' => 'http://restapi.amap.com/v3/geocode/geo?key=xxxxxxxxxxxxxxxx&address=%s&city=%s'
];
3、在这里我是才用了添加信息成功后,进行地址转换操作,后修改数据库的经纬度字段中的内容。
经度:longitude
纬度:latitude
// 添加房源信息处理
public function store(FangRequest $request) {
// 表单数据
$dopost = $request->except(['_token', 'file']);
// 入库
$model = Fang::create($dopost);
// 添加数据入库成功了
// 发起HTTP请求
// 申明一个请求类,并指定请求的过期时间
$client = new Client(['timeout' => 5]);
// 得到请求地址
$url = config('gaode.geocode');
$url = sprintf($url, $model->fang_addr, $model->fang_province);
// 发起请求
$response = $client->get($url);
$body = (string)$response->getBody