{"message":"无法通过center得到地理位置","status":"504"}
来源:6-2 根据经纬度或者地址调取百度地图
![](http://img1.sycdn.imooc.com/user/5448f32b00015f0101000100-100-100.jpg)
迎风向上乀
2018-02-16
//http://api.map.baidu.com/staticimage/v2 /** * 根据经纬度或者地址来获取百度地图 * @param $center */ public static function staticimage($center) { if(!$center) { return ''; } $data = [ 'ak' => config('map.ak'), 'width' => config('map.width'), 'height' => config('map.height'), 'center' => $center, 'markers' => $center, ]; $url = config('map.baidu_map_url').config('map.staticimage').'?'.http_build_query($data); // 1 file_get_contents($url); // 2 curl $result = doCurl($url); return $result; }
<?php /** * Created by PhpStorm. * User: chenshu * Date: 2018/2/16 * Time: 下午1:07 */ return [ 'ak' => 'RCqr5risuryquwlO8FINvNVwzYMRpbsq', 'baidu_map_url' => 'http://api.map.baidu.com/', 'geocoder' => 'geocoder/v2/', 'width' => 400, 'height' => 300, 'staticimage' => 'staticimage/v2', ];
public function map(){ return \Map::staticimage('北京昌平河沙地铁'); }
/** * @param $url * @param int $type 0 get 1 post * @param array $data */ function doCurl($url, $type=0, $data=[]) { $ch = curl_init(); // 初始化 // 设置选项 curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER,0); if($type == 1) { // post curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); } //执行并获取内容 $output = curl_exec($ch); // 释放curl句柄 curl_close($ch); return $output; }
写回答
1回答
-
WL伟霖
2018-02-18
你代码中地址写错了。。。视频中的地址是:“北京昌平沙河地铁”,你的是:“北京昌平河沙地铁”。如果输入的地址是不存在的话,api是返回状态码504
112018-02-22
相似问题