一. 创建config配置
gaode.php
return [
'geocode' => 'https://restapi.amap.com/v3/geocode/geo?key=你的key&address=%s&city=%s'
];
二. 安装Guzzle-http插件
Guzzle-http: Guzzle, PHP HTTP 客户端 — Guzzle中文文档
composer require guzzlehttp/guzzle
三. 创建自定义函数
在app目录下创建common.php
// 通过高德接口获得经纬度
function coordinate($addr, $city)
{
$client = new \GuzzleHttp\Client();
$url = config('gaode.geocode');
$url = sprintf($url, $addr, $city);
$res = $client->request('GET', $url);
$body = (string)$res->getBody();
$arr = json_decode($body, true);
if (count($arr['geocodes']) > 0) {
$locationArr = explode(',', $arr['geocodes'][0]['location']);