CusAccessObjectUtil2.java 3.1 KB
package org.theyeasy.weixin.util;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.alibaba.fastjson.JSONObject;
import com.w1hd.zzhnc.util.CommonUtil;
import com.w1hd.zzhnc.util.JsonMapper;

public class CusAccessObjectUtil2 {
	public static String getIpAddress(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("HTTP_CLIENT_IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("HTTP_X_FORWARDED_FOR");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip;
	}

	public static String getIpCity(HttpServletRequest request) {
		String sendGet = CommonUtil.sendGet("http://ip.taobao.com/service/getIpInfo.php?ip=", getIpAddress(request));
		JSONObject json = JSONObject.parseObject(sendGet);
		if (json.containsKey("city")) {
			return json.get("city").toString();
		}
		return null;

	}

	public static String getCityName(double lng, double lat) {
		JSONObject jsonObject = locationResult(lng, lat);
		JSONObject jsonObject2 = jsonObject.getJSONObject("addressComponent");
		String string = jsonObject2.getString("city");
		System.out.println(string);
		return string;
	}

	public static JSONObject locationResult(double lng, double lat) {
		if (lng == 0 || lat == 0)
			return null;
		String s = CommonUtil.sendGet("http://api.map.baidu.com/geocoder/v2/?callback=renderReverse&location=" + lat
				+ "," + lng + "&output=json&pois=1&ak=S1NCGtjD2Sd46UAxt9sz9Wj20Fr0joqu", null);
		String substring = s.substring(0, s.length() - 1);
		System.out.println(s);
		String replace = substring.replace("renderReverse&&renderReverse(", "");
		JSONObject parse = (JSONObject) JSONObject.parse(replace);
		JSONObject jsonObject = parse.getJSONObject("result");
		return jsonObject;
	}

	public static String getProvince(double lng, double lat) {
		JSONObject jsonObject = locationResult(lng, lat);
		JSONObject jsonObject2 = jsonObject.getJSONObject("addressComponent");
		String string = jsonObject2.getString("province");
		return string;
	}

	public static String getProvince(JSONObject jsonObject) {
		JSONObject jsonObject2 = jsonObject.getJSONObject("addressComponent");
		String string = jsonObject2.getString("province");
		return string;
	}

	public static String getCity(JSONObject jsonObject) {
		JSONObject jsonObject2 = jsonObject.getJSONObject("addressComponent");
		String string = jsonObject2.getString("city");
		return string;
	}

	public static String getDistrict(JSONObject jsonObject) {
		JSONObject jsonObject2 = jsonObject.getJSONObject("addressComponent");
		String string = jsonObject2.getString("district");
		return string;
	}
	
	public static String getFormattedAddress(JSONObject jsonObject) {
		return jsonObject.getString("formatted_address");
	}
}