绘制静态地图API-高德地图

1 高德静态地图链接

http://lbs.amap.com/api/webservice/guide/api/staticmaps

2

/**
* User: [一秋]
* Date: 2017/9/4
* Time: 10:17
* Desc: 成功来源于点滴
*/ namespace app\lib\bin;
//高德地图API
header("Content-Type: text/html; charset=utf-8");
class Amap
{
private $web_service_key;
private $host ;
public function __construct()
{
$this->web_service_key = 'your web key';//web服务key值
$this->host = request()->host();
} //静态地图
public function staticMap($s_lng,$s_lat,$g_lng,$g_lat){ $urlObj['size'] = "710*430";
// $urlObj['zoom'] = 15;
$urlObj['scale'] = 1;
$urlObj['traffic'] = 0;
// $urlObj['paths'] = '5,0x0000FF,1,,:113.709999,34.729698;113.740639,34.761528';
$urlObj['markers'] = "-1,定位图标图片链接,0:$s_lng,$s_lat|-1,定位图标图片链接,0:$g_lng,$g_lat";
$urlObj['key'] = $this->web_service_key;
$bizString = $this->ToUrlParams($urlObj); $url = "http://restapi.amap.com/v3/staticmap?".$bizString; $info = $this->curl_get($url); $img_path = $this->generateImg($info); return $this->host.'/'.$img_path;
} private function generateImg($img_info){
$ret = [];
$year = date('Ymd');
$path = "uploads/$year";
if (!is_dir($path)) {
mkdir(iconv("UTF-8", "GBK", $path), 0777, true);
}
$imgname = 'amap'.$this->getMillisecond();
$type = 'png';
$new_file = $path.'/'.$imgname.".".$type; $a = file_put_contents($new_file,$img_info);
if ($a) {
return $path . '/' . $imgname . "." . $type;
}
return $ret;
} /**
* 拼接字符串
*/
private function ToUrlParams($urlObj)
{
$buff = "";
foreach ($urlObj as $k => $v)
{
if($k != "sign"){
$buff .= $k . "=" . $v . "&";
}
} $buff = trim($buff, "&");
return $buff;
} /**
* @param string $url get请求地址
* @param int $httpCode 返回状态码
* @return mixed
*/
private function curl_get($url, &$httpCode = 0)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //不做证书校验,部署在linux环境下请改为true
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
$file_contents = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return $file_contents;
}
// 获取毫秒值
private function getMillisecond()
{
list ($t1, $t2) = explode(' ', microtime());
return ( float )sprintf('%.0f', (floatval($t1) + floatval($t2)) * 1000);
} }
上一篇:登录+爬取淘宝商品信息+字典用json存储


下一篇:爬取淘宝商品信息,放到html页面展示