<?php

namespace app\common\exception;

/**
 * 地图扩展类
 */
class Map extends \Exception
{
    /**
     * 通过地址名获取经纬度
     * @address 地址
     */
    public static function getLongitudeAndLatitude($address)
    {
        //创建应用生成的key
        $key = '5f059c7c7346c00474804e260979e0b3';
        //根据开发文档获取经纬度
        $url = "https://restapi.amap.com/v3/geocode/geo?key=" . $key . "&address=" . $address;
        $json = file_get_contents($url);
        $GaoDeArr = json_decode($json, true);
        
        if ($GaoDeArr['infocode'] != '10000') {
            return throwErrorMsg("{$address} 地址信息输入错误");
        }
        $location = explode(',', $GaoDeArr['geocodes'][0]['location']);
        //经度
        $longitude = $location[0];
        //纬度
        $latitude = $location[1];

        return [
            'longitude' => $longitude,
            'latitude' => $latitude,
        ];
    }
}
