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

Posted wqy的笔记

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了绘制静态地图API-高德地图相关的知识,希望对你有一定的参考价值。

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-高德地图的主要内容,如果未能解决你的问题,请参考以下文章

如何利用JavaScript API绘制高德地图

百度地图,怎么可以显示图标的呢??

百度地图绘制运动轨迹技巧

高德地图获取道路接口并将道路绘制

Android 绘制中国地图

高德地图驾车路线规划怎么自己绘制线路