package com.sifang.modules.order.utils;


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;



public class Apiutils {

	private static String API = "http://restapi.amap.com/v3/geocode/geo?key=<key>&s=rsv3&address=<address>";
	
	private static String KEY = "727b25e7366ab5015225754e8ee00fef";

	private static final String DEFAULT_CHARSET = "UTF-8";
    
    private static Pattern pattern = Pattern.compile("\"location\":\"(\\d+\\.\\d+),(\\d+\\.\\d+)\"");

	static {
		init();
	}

	private static void init() {
		System.out.println("高德地图工具类初始化");
		System.out.println("api: {}"+API);
		System.out.println("key: {}"+KEY);
		API = API.replaceAll("<key>", KEY);
	}

	/**
	 * 接口调用 GET
	 */
	public static double[] httpURLConectionGET(String address) {
		try {
			String requestUrl = API.replaceAll("<address>", URLEncoder.encode(address, DEFAULT_CHARSET));
			URL url = new URL(requestUrl);    // 把字符串转换为URL请求地址
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();// 打开连接
			connection.connect();// 连接会话
			// 获取输入流
			BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
			String line;
			StringBuilder sb = new StringBuilder();
			while ((line = br.readLine()) != null) {// 循环读取流
				sb.append(line);
			}
			br.close();// 关闭流
			connection.disconnect();// 断开连接
			Matcher matcher = pattern.matcher(sb.toString());
			if (matcher.find() && matcher.groupCount() == 2) {
				double[] gps = new double[2];
				gps[0] = Double.valueOf(matcher.group(1));
				gps[1] = Double.valueOf(matcher.group(2));
				System.out.println(gps.toString());
				return gps;
			}
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("失败!");
			return null;
		}
		return null;
	}
    
    public static double[] addressToGPS(String address) {
    	String add = address.replace(" ", "");
    	int sta = address.replace(" ", "").indexOf("#");
    	String addres ;
    	if(sta != -1){
    		addres = add.substring(0, (sta-1));
    	}else{
    		addres= add;
    	}
        try {
			String url = API.replaceAll("<address>", URLEncoder.encode(addres, DEFAULT_CHARSET));
            URL myURL = null;
            URLConnection httpsConn = null; 
	    	try { 
	    		myURL = new URL(url); 
	    	} catch (MalformedURLException e) { 
	    		e.printStackTrace(); 
	    	} 
	    	InputStreamReader insr = null;
	    	BufferedReader br = null;
	    	httpsConn = (URLConnection) myURL.openConnection();// 不使用代理 
	    	if (httpsConn != null) {
		    	insr = new InputStreamReader( httpsConn.getInputStream(), "UTF-8");
		    	br = new BufferedReader(insr); 
		    	String data = "";
		    	String line = null; 
		    	while((line= br.readLine())!=null){
		    		data+=line;
		    	} 
	            Matcher matcher = pattern.matcher(data);  
	            if (matcher.find() && matcher.groupCount() == 2) {
	                double[] gps = new double[2];
	                gps[0] = Double.valueOf(matcher.group(1));  
	                gps[1] = Double.valueOf(matcher.group(2));
	                return gps;
	            }
	    	}
        }catch (Exception e) {
        	e.printStackTrace(); 
        	return null;
		}
        return null;
    }
    
    public static void main(String[] args) {  
    	double [] data = Apiutils.addressToGPS("四川省达州市达川区碗厂村");
    	System.out.println(data.toString());
    }
	
}
