PHP--地球坐标系转为火星坐标系再转为高德坐标系

    /**
     * 地球坐标系转为高德坐标系
     * @param $earthLog 11847.6596
     * @param $earthLat 3156.7211
     * @return array
     */
    public static function earthToAmap($earthLog, $earthLat)
    {
        sscanf($earthLog, '%3d%7f', $earthLog_degrees, $earthLog_minutes);
        $marsLog = $earthLog_degrees + $earthLog_minutes / 60; //火星经度
        sscanf($earthLat, '%2d%7f', $earthLat_degrees, $earthLat_minutes);
        $marsLat = $earthLat_degrees + $earthLat_minutes / 60; //火星纬度
        //再转成高德坐标系
        $key = config('maodu.amap_key');
        $data = "key=$key&coordsys=gps&locations=$marsLog,$marsLat";
        $amap = self::curl_get('https://restapi.amap.com/v3/assistant/coordinate/convert?' . $data);
        $amap = json_decode($amap, true);
        $locations = $amap['locations'];
        //获取详细地址
        $url = "https://restapi.amap.com/v3/geocode/regeo?&key=$key&location=$locations";
        $address = self::curl_get($url);
        $address = json_decode($address, true);
        return ['location' => $locations, 'desc' => $address['regeocode']['formatted_address']];
    }


    public static function curl_get($url)
    {

        $header = array(
            'Accept: application/json',
        );
        $curl = curl_init();
        //设置抓取的url
        curl_setopt($curl, CURLOPT_URL, $url);
        //设置头文件的信息作为数据流输出
        curl_setopt($curl, CURLOPT_HEADER, 0);
        // 超时设置,以秒为单位
        curl_setopt($curl, CURLOPT_TIMEOUT, 1);

        // 超时设置,以毫秒为单位
        // curl_setopt($curl, CURLOPT_TIMEOUT_MS, 500);

        // 设置请求头
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
        //设置获取的信息以文件流的形式返回,而不是直接输出。
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        //执行命令
        $data = curl_exec($curl);
        return $data;
//        // 显示错误信息
//        if (curl_error($curl)) {
//            print "Error: " . curl_error($curl);
//        } else {
//            // 打印返回的内容
//            var_dump($data);
//            curl_close($curl);
//        }
    }

 

你可能感兴趣的:(PHP)