<?php
/**
 * Created by PhpStorm.
 * User: Jun.peng
 * Date: 2019/6/19
 * Time: 15:53
 */
namespace map;

class Map
{
    //根据地址获取经纬度
    public static function getLngLat($address)
    {
        // http://api.map.baidu.com/geocoder/v2/?address=北京市海淀区上地十街10号&output=json&ak=您的ak&callback=showLocation //GET请求
        $data = [
            'address' => $address,
            'ak' => 'EglSDP3UNYtT2GsjGsoBFrHYBfixPrDv',
            'output' => 'json',
        ];
        //转化为网址形式
        $url = 'http://api.map.baidu.com/geocoder/v2/?' . http_build_query($data);
        $res = self::doCurl($url);
        $res = json_decode($res,true);
        return $res['result'];
    }

    //根据经纬度或者地址获取百度地图
    public static function staticimage($center)
    {
        if (!$center) {
            return '';
        }
        $data = [
            'ak' => 'EglSDP3UNYtT2GsjGsoBFrHYBfixPrDv',
            'width' => '100',
            'height' => '200',
            'center' => $center,
            'markers' => $center,
        ];
        $url = 'http://api.map.baidu.com/geocoder/v2/?' . http_build_query($data);
        $res = self::doCurl($url);
        return $res;
    }

    public static 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_PORT, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        }
//执行获取内容
        $output = curl_exec($ch);
//释放curl句柄
        curl_close($ch);
        return $output;
    }


    /**
     * 【腾讯地图】
     * 【根据详细地址获取经纬度】
     *  20170920
     *
     * @param $address
     * @return array
     */
    public static function getPoint($address)
    {
        $url = "https://apis.map.qq.com/ws/geocoder/v1/?address=" . $address . "&key=5ULBZ-2M4LJ-QS3FO-F6SSW-X3CRK-WABPO";
        $res = self::doCurl($url);

        return $res;
    }
}