import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.io.IOUtils;
import org.apache.http.util.TextUtils;
import org.codehaus.janino.UnicodeUnescapeReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;
/**
* 地址工具,使用QQ地图API转换经纬度位地址信息
* 使用淘宝工具转换IP位地址信息
*/
public class GeoUtils {
private static Logger logger = LoggerFactory.getLogger(GeoUtils.class.getName());
private final static String QQ_MAP_API_KEY = ""; //此处暂时省略真实key
private final static String QQ_MAP_API_URL = "http://apis.map.qq.com/ws/geocoder/v1";
private final static String TAOBAO_API_IP_URL = "http://ip.taobao.com/service/getIpInfo.php";
private final static String coordinate = "";
/**
* 由经纬度换成地址,使用QQ地图
*
* @param coordinate , location=lat<纬度>,lng<经度>
* @return
*/
public static Address coordinate2Address(String coordinate) {
Map<String, String> params = new HashMap<String, String>();
params.put("location", coordinate);
params.put("coord_type", "1");
params.put("get_poi", "0");
params.put("key", QQ_MAP_API_KEY);
params.put("output", "json");
String resp = null;
try {
resp = LambdaHttpClientUtils.doGet(QQ_MAP_API_URL, params);
logger.info("reqest param , coordinate = {}, QQ MAP response = {}", coordinate, resp);
} catch (Exception e) {
logger.error("请求QQ地图异常", e);
}
return parseQQMapResp(resp);
}
/**
* 使用TAOBAO服务,由ip换成地址
*
* @param ip
* @return
*/
public static Address ip2Address(String ip) {
Map<String, String> params = new HashMap<String, String>();
params.put("ip", ip);
String resp = null;
try {
resp = LambdaHttpClientUtils.doGet(TAOBAO_API_IP_URL, params);
UnicodeUnescapeReader unescapeReader = new UnicodeUnescapeReader(new StringReader(resp));
String readableStr = IOUtils.toString(unescapeReader);
unescapeReader.close();
logger.info("request param, ip = {}, taobao ipserver response = {} ", ip, readableStr);
} catch (Exception e) {
logger.error("请求淘宝异常", e);
}
return parseTaobaoIpResp(resp);
}
/**
* 解析QQ地图的响应消息
*
* @param resp
* @return
*/
private static Address parseQQMapResp(String resp) {
String address = "";
String province = "";
String city = "";
String district = "";
if (!TextUtils.isEmpty(resp)) {
ObjectMapper objectMapper = new ObjectMapper();
try {
JsonNode root = objectMapper.readTree(resp);
String status = root.get("status").asText();
if ("0".equals(status)) {
JsonNode jo = root.get("result");
address = jo.get("address").asText();
province = jo.get("ad_info").get("province").asText();
city = jo.get("ad_info").get("city").asText();
district = jo.get("ad_info").get("district").asText();
if (province.equals(city)) {
city = district;
}
logger.info("user address = " + address);
}
} catch (IOException e) {
logger.info("请求消息 {}", resp);
logger.error("解析qq地图响应消息异常", e);
}
}
return new Address(address, province, city, district);
}
private static Address parseTaobaoIpResp(String resp) {
String address = "";
String province = "";
String city = "";
String district = "";
if (!TextUtils.isEmpty(resp)) {
ObjectMapper objectMapper = new ObjectMapper();
try {
JsonNode root = objectMapper.readTree(resp);
String status = root.get("code").asText();
if ("0".equals(status)) {
JsonNode jo = root.get("data");
province = jo.get("region").asText();
city = jo.get("city").asText();
district = jo.get("county").asText();
address = province + city + district;
logger.info("user address = " + address);
}
} catch (IOException e) {
logger.info("请求消息 {}", resp);
logger.error("解析淘宝响应消息异常", e);
}
}
return new Address(address, province, city, district);
}
/**
* 地址和城市代码
*/
public static class Address {
private String address;
private String province;
private String city;
private String district;
public Address(String address, String city) {
super();
this.address = address;
this.city = city;
}
public Address(String address, String province, String city, String district) {
super();
this.address = address;
this.province = province;
this.city = city;
this.district = district;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getDistrict() {
return district;
}
public void setDistrict(String district) {
this.district = district;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override public String toString() {
return "Address [address=" + address + ", province=" + province + ", city=" + city + ", district=" + district + "]";
}
}
}