package com.zhongdao.jlr.util;



import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.alibaba.fastjson.JSON;

public class GeoAddressCenter {
	private static Log log = LogFactory.getLog(GeoAddressCenter.class);
	public static final String KEY = "7de40e4d33458a1c5901c3935f916d3c";  //个人KEY
	
	
	//public static final String KEY = "41e95051d96ed69493f46e435204dc56";   //企业KEY
	/**
	 * 根据经纬度查询位置信息
	 * @param locaX
	 * @param locaY
	 * @return
	 */
	public static String getGeoAddress(String locaX,String locaY){
		String geoAddress = "";
		try {
		    String route_url = " http://restapi.amap.com/v3/geocode/regeo?output=json&radius=1000";

			if (locaX == null || locaY == null || locaX.equals("") || locaY.equals("")) {
	 
			}else{
			    route_url += "&location="+locaX+","+locaY +"&key="+ KEY;
			    //System.out.println(route_url);
				HttpClientDownload httpDown = new HttpClientDownload();
				httpDown.setFileUrl(route_url);
				if (httpDown.download()) {
					byte[] httpb = httpDown.getData();
					String data = new String(httpb, "utf-8");
					//System.out.println(data);
					if(data!=null && !"".equals(data)){
						Map map = (Map) JSON.parse(data);
						if (map.get("status") != null&& !map.get("status").equals("")) {
							if (map.get("status").equals("1")) {
								Map geoMap = (Map)map.get("regeocode");
								if(geoMap != null){
									geoAddress = (String)geoMap.get("formatted_address");
								}
							} 
						}
					}			
				} 
			}
		} catch (Exception ee) {
		  	log.error("", ee);
		}
		 
		return geoAddress;
	}
	
	/**
	 * 根据经纬度查询地理编码
	 * @param locaX
	 * @param locaY
	 * @return
	 */
	public static String getGeoAdcode(String locaX,String locaY){
		String adCode = "000000";
		try {
		    String route_url = " http://restapi.amap.com/v3/geocode/regeo?output=json&radius=1000";

			if (locaX == null || locaY == null || locaX.equals("") || locaY.equals("")) {
	 
			}else{
			    route_url += "&location="+locaX+","+locaY +"&key="+ KEY;
			    //System.out.println(route_url);
				HttpClientDownload httpDown = new HttpClientDownload();
				httpDown.setFileUrl(route_url);
				if (httpDown.download()) {
					byte[] httpb = httpDown.getData();
					String data = new String(httpb, "utf-8");
					//System.out.println(data);
					if(data!=null && !"".equals(data)){
						Map map = (Map) JSON.parse(data);
						if (map.get("status") != null&& !map.get("status").equals("")) {
							if (map.get("status").equals("1")) {
								Map geoMap = (Map)map.get("regeocode");
								if(geoMap != null){
									Map addressMap = (Map)geoMap.get("addressComponent");
									if(addressMap != null){
										adCode = (String)addressMap.get("adcode");
										if(adCode==null){
											adCode = "000000";
										}
									}
								}
							} 
						}
					}			
				} 
			}
		} catch (Exception ee) {
		  	log.error("", ee);
		}
		 
		return adCode;
	}
	
	/**
	 * 根据地理名称查询区域代码
	 * @param addressName
	 * @return
	 */
	public static String getGeoAdcode(String addressName){
		String adCode = "000000";
		try {
		    String route_url = "http://restapi.amap.com/v3/geocode/geo?";

			if (addressName != null || !addressName.equals("")) {
				route_url += "address="+URLEncoder.encode(addressName, "utf-8")+"&key="+ KEY+"&output=json";
				    
				HttpClientDownload httpDown = new HttpClientDownload();
				httpDown.setFileUrl(route_url);
				if (httpDown.download()) {
					byte[] httpb = httpDown.getData();
					String data = new String(httpb, "utf-8");
					if(data!=null && !"".equals(data)){
						Map map = (Map) JSON.parse(data);
						if (map.get("status") != null&& !map.get("status").equals("")) {
							if (map.get("status").equals("1")) {
								List geoList = (List)map.get("geocodes");
								if(geoList!=null && geoList.size()>0){
									Map sMap = (Map)geoList.get(0);
									adCode = (String)sMap.get("adcode");
								}
							} 
						}
					}	
				} 	 
			}

		} catch (Exception ee) {
		  	log.error("", ee);
		}
		 
		return adCode;
	}
	
	/**
	 * 根据地理名称查询经纬度
	 * @param addressName
	 * @return
	 */
	public static String getGeoLonLat(String addressName){
		String locationXY = "";
		try {
		    String route_url = "http://restapi.amap.com/v3/geocode/geo?";

			if (addressName != null || !addressName.equals("")) {
				route_url += "address="+URLEncoder.encode(addressName, "utf-8")+"&key="+ KEY+"&output=json";
				    
				HttpClientDownload httpDown = new HttpClientDownload();
				httpDown.setFileUrl(route_url);
				if (httpDown.download()) {
					byte[] httpb = httpDown.getData();
					String data = new String(httpb, "utf-8");
					System.out.println(data);
					if(data!=null && !"".equals(data)){
						Map map = (Map) JSON.parse(data);
						if (map.get("status") != null&& !map.get("status").equals("")) {
							if (map.get("status").equals("1")) {
								List geoList = (List)map.get("geocodes");
								if(geoList!=null && geoList.size()>0){
									Map sMap = (Map)geoList.get(0);
									locationXY = (String)sMap.get("location");
								}
							} 
						}
					}	
				} 	 
			}

		} catch (Exception ee) {
		  	log.error("", ee);
		}
		 
		return locationXY;
	}
	
	public static String getGeoJson(String addressName){
		String locationXY = "";
		try {
		    String route_url = "http://restapi.amap.com/v3/geocode/geo?";

			if (addressName != null || !addressName.equals("")) {
				route_url += "address="+URLEncoder.encode(addressName, "utf-8")+"&key="+ KEY+"&output=json";
				//log.info(route_url);   
				HttpClientDownload httpDown = new HttpClientDownload();
				httpDown.setFileUrl(route_url);
				if (httpDown.download()) {
					byte[] httpb = httpDown.getData();
					String data = new String(httpb, "utf-8");
					if(data!=null && !"".equals(data)){
						Map map = (Map) JSON.parse(data);
						if (map.get("status") != null&& !map.get("status").equals("")) {
							if (map.get("status").equals("1")) {
								List geoList = (List)map.get("geocodes");
								if(geoList!=null && geoList.size()>0){
									Map sMap = (Map)geoList.get(0);
									locationXY = (String)sMap.get("location")+","+(String)sMap.get("adcode");
								}
							} 
						}
					}	
				} 	 
			}

		} catch (Exception ee) {
			ee.printStackTrace();
		  	log.error("", ee);
		}
		System.out.println(locationXY);
		return locationXY;
	}
	
	public static void main(String[] atgs){
		String dd = GeoAddressCenter.getGeoAdcode("121.397358","31.623553");
//		String dd = GeoAddressCenter.getGeoLonLat("浙江省嘉兴市桐乡市屠甸出口");
		System.out.println(dd);
		
	}
	

}
