PHP(Laravel&ThinkPHP)+百度实现地址转经纬度

 1. 使用PHP客户端GuzzleHttp

  下载GuzzleHttp  

composer require guzzlehttp/guzzle

2. 代码

use GuzzleHttp\Client;
// $addr为地址
// config('addr')=>http://api.map.baidu.com/geocoding/v3/?address=%s&output=json&ak=CxOKZ1tLK5U97ytZFFG6H2kEKTSPpMSm&callback=showLocation
class Latlon{
    public static function getinfo($addr)
        {
            $url = sprintf(config('addr'),$addr);
            $client = new Client(['timeout' => 5,'verify' => false]);
            $response = $client->get($url);
            $json = (string)$response->getBody();
            $v['lng'] = substr(explode('lng":',$json)[1],0,10);
            $v['lat'] = substr(explode('lat":',$json)[1],0,10);
            dd($v);
        }
}

上一篇:关于地图坐标问题转换


下一篇:vue结合百度地图实现热力图