码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2017-09-25 16:41:18      阅读:981      评论:0      收藏:0      [点我收藏+]

标签:tap   gbk   set   har   connect   位图   rest   img   高德地图   

1 高德静态地图链接 

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

 

<?php
/**
 * 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);
    }

}

 

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

标签:tap   gbk   set   har   connect   位图   rest   img   高德地图   

原文地址:http://www.cnblogs.com/wqy415/p/7592552.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!