package com.ztmap.project.common;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ztmap.common.utils.http.HttpUtils;
import com.ztmap.project.common.domain.GPS;

/**
 * 高德地图Api
 * 工具
 */
public class GaudMapUtils {

    private static String key = "52b2d91894db0929c25d9ee1284b14d2";

    public static String getGaudCoordinate(String address){
        String location = "";
        String result = HttpUtils.sendGet("https://restapi.amap.com/v3/geocode/geo",
                String.format("address=%s&key=%s", address, key)
        );
        JSONObject result_json = JSONObject.parseObject(result);
        if (result_json.getString("status").equals("1")) {
            JSONArray geocodes_array = result_json.getJSONArray("geocodes");
            JSONObject geocodes_json = JSONObject.parseObject(geocodes_array.get(0).toString());
            location = geocodes_json.getString("location");
        }

        System.out.println("经度："+location.split(",")[0]);
        System.out.println("维度："+location.split(",")[1]);
        System.out.println(result);
        return location;
    }


    public static void main(String[] args) {
        String str = "聊城市冠县红旗路冠县人民政府东南侧约30米,聊城市冠县斜店乡中心幼儿园分园东北侧约250米,聊城市冠县贾镇政府院内东侧一楼,聊城市冠县西环路世纪家园南侧约60米,聊城市冠县红旗路1号清水镇人民政府院内,聊城市冠县岳杨路6号万善乡政府院内,聊城市冠县北陶安居花园西南侧约110米,聊城市冠县班桑路梁堂中学西侧约30米,聊城市冠县和谐路,聊城市冠县斜店乡中心幼儿园分园东北侧约250米";
        String[] split = str.split(",");
        for (String s : split) {
            String gaudCoordinate = getGaudCoordinate(s);
            Double x = Double.valueOf(gaudCoordinate.split(",")[0]);
            Double y = Double.valueOf(gaudCoordinate.split(",")[1]);
            GPS gps = GPSConverterUtils.gcj_To_Gps84(x, y);
            System.out.println("地址："+s+" 点位：{x:"+gps.getLat()+",y:"+gps.getLon()+"}");

        }

    }
}
