package com.platform.yijia.utils;

import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

/**
 * <Title> BaiduMapUtil </Title>
 * <Decription> 百度地图经纬度获取地址工具类 </Decription>
 * <Date> 2021年7月28日</Date>
 * <Author> JK </Author>
 */
public class BaiduMapUtil {
    private static Logger logger =(Logger) LoggerFactory.getLogger(BaiduMapUtil.class);
    //百度地图ak值
    private static final  String AK = "j9CtICRKVd3i0qBo4bFn9qHt5E7TzQDK";

    /*
     * 获取位置信息
     * @param lng 经度值
     * @param lat 纬度值
     * @return
     */
    public static JSONObject getLocation(String lng, String lat){
        String url = "http://api.map.baidu.com/reverse_geocoding/v3/?ak="+AK+"&output=json&coordtype=wgs84ll&location="+lat+","+ lng;
        JSONObject obj = JSONObject.fromObject(HttpRequest(url));
        return obj;
    }

    //发送请求
    private static String HttpRequest (String url) {
        StringBuilder json = new StringBuilder();
        try {
            URL oracle = new URL(url);
            URLConnection yc = oracle.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream(),"UTF-8"));
            String inputLine = null;
            while ( (inputLine = in.readLine()) != null) {
                json.append(inputLine);
            }
            logger.info("百度地图获取位置信息成功：" + json);
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("百度地图获取信息失败：" + e.getMessage());
        }
        return json.toString();
    }
}
