use of com.quickutil.platform.entity.GeoDef in project quickutil by quickutil.
the class GeoUtil method geoGpsByAmap.
/**
* 根据经纬度查询地理信息-高德中国
*/
public static GeoDef geoGpsByAmap(double latitude, double longitude) {
try {
double[] delta = WGSToGCJPointer(latitude, longitude);
String queryUrl = String.format("http://restapi.amap.com/v3/geocode/regeo?output=json&location=%s,%s&key=%s", delta[1], delta[0], amapKeyIn);
JsonObject object = JsonUtil.toJsonObject(FileUtil.stream2string(HttpUtil.httpGet(queryUrl).getEntity().getContent()));
String country = object.getAsJsonObject("regeocode").getAsJsonObject("addressComponent").get("country").getAsString();
if (country.equals("中国")) {
country = "China";
}
if (country.equals("England")) {
country = "United Kingdom";
}
String city = "";
if (!object.getAsJsonObject("regeocode").getAsJsonObject("addressComponent").get("city").isJsonArray()) {
city = object.getAsJsonObject("regeocode").getAsJsonObject("addressComponent").get("city").getAsString();
}
String description = object.getAsJsonObject("regeocode").get("formatted_address").getAsString();
String countryCode = countryCodeByCountryName(country);
String countryChinese = countryChineseByCountryCode(countryCode);
String state = object.get("province").getAsString();
String stateCode = "";
if (country.equals("China")) {
stateCode = stateCodeByStateChinese(countryCode, state);
} else {
stateCode = stateCodeByStateName(countryCode, state);
}
state = stateNameByStateCode(countryCode, stateCode);
String stateChinese = stateChineseByStateCode(countryCode, stateCode);
countryCode = (countryCode == null) ? UNKNOWN : countryCode;
country = (country == null) ? UNKNOWN : country;
countryChinese = (countryChinese == null) ? UNKNOWN : countryChinese;
stateCode = (stateCode == null) ? UNKNOWN : stateCode;
state = (state == null) ? UNKNOWN : state;
stateChinese = (stateChinese == null) ? UNKNOWN : stateChinese;
city = (city == null) ? UNKNOWN : city;
return new GeoDef(latitude, longitude, countryCode, country, countryChinese, stateCode, state, stateChinese, city, description);
} catch (Exception e) {
LOGGER.error(Symbol.BLANK, e);
}
return null;
}
use of com.quickutil.platform.entity.GeoDef in project quickutil by quickutil.
the class GeoUtil method geoGpsByOfflineDB.
public static GeoDef geoGpsByOfflineDB(double latitude, double longitude) {
float latF = Float.valueOf(new DecimalFormat("#.00").format(latitude));
float lngF = Float.valueOf(new DecimalFormat("#.00").format(longitude));
Integer idx = geodbCacheMap.get(latF + "/" + lngF);
if (idx == null) {
latF = Float.valueOf(new DecimalFormat("#.0").format(latitude));
lngF = Float.valueOf(new DecimalFormat("#.0").format(longitude));
idx = geodbCacheMap.get(latF + "/" + lngF);
if (idx == null) {
LOGGER.info("No result, lat: " + latitude + " lng: " + longitude);
return new GeoDef(latitude, longitude, "", "", "", "", "", "", "", "");
}
}
String content = geodbQueryMap.get(idx);
String[] results = new String[8];
String[] items = content.split(",");
for (int i = 0; i < items.length; i++) {
results[i] = items[i];
}
for (int i = items.length; i < results.length; i++) {
results[i] = "";
}
return new GeoDef(latitude, longitude, results[0], results[1], results[2], results[3], results[4], results[5], results[6], results[7]);
// String countryCode;
// String country;
// String countryChinese;
// String stateCode;
// String state;
// String stateChinese;
// String city;
// String description;
}
use of com.quickutil.platform.entity.GeoDef in project quickutil by quickutil.
the class GeoUtil method geoIPByMMDB.
public static GeoDef geoIPByMMDB(String ip) {
String countryCode = UNKNOWN;
String country = UNKNOWN;
String countryChinese = UNKNOWN;
String stateCode = UNKNOWN;
String state = UNKNOWN;
String stateChinese = UNKNOWN;
String city = UNKNOWN;
Double latitude = 0.0;
Double longitude = 0.0;
try {
InetAddress ipAddr = InetAddress.getByName(ip);
CityResponse result;
result = mmdbReader.city(ipAddr);
countryCode = result.getCountry().getIsoCode();
country = result.getCountry().getName();
countryChinese = countryChineseByCountryCode(countryCode);
stateCode = result.getMostSpecificSubdivision().getIsoCode();
state = stateNameByStateCode(countryCode, stateCode);
stateChinese = stateChineseByStateCode(countryCode, stateCode);
stateCode = stateCodeByStateCodeNewVersion(countryCode, stateCode);
city = result.getCity().getName();
if (countryCode != null && countryCode.equals("CN") && city != null && result.getCity().getNames().containsKey("zh-CN")) {
if (!result.getCity().getNames().get("zh-CN").endsWith("市")) {
city = result.getCity().getNames().get("zh-CN") + "市";
} else {
city = result.getCity().getNames().get("zh-CN");
}
}
latitude = result.getLocation().getLatitude();
longitude = result.getLocation().getLongitude();
} catch (AddressNotFoundException ae) {
} catch (UnknownHostException ue) {
} catch (Exception e) {
LOGGER.error(Symbol.BLANK, e);
}
countryCode = (countryCode == null) ? UNKNOWN : countryCode;
country = (country == null) ? UNKNOWN : country;
countryChinese = (countryChinese == null) ? UNKNOWN : countryChinese;
stateCode = (stateCode == null) ? UNKNOWN : stateCode;
state = (state == null) ? UNKNOWN : state;
stateChinese = (stateChinese == null) ? UNKNOWN : stateChinese;
city = (city == null) ? UNKNOWN : city;
latitude = (latitude == null) ? 0.0 : latitude;
longitude = (longitude == null) ? 0.0 : longitude;
return new GeoDef(latitude, longitude, countryCode, country, countryChinese, stateCode, state, stateChinese, city, "");
}
use of com.quickutil.platform.entity.GeoDef in project quickutil by quickutil.
the class GeoUtil method geoGpsByBaidu.
/**
* 根据经纬度查询地理信息-百度世界
*/
public static GeoDef geoGpsByBaidu(double latitude, double longitude) {
try {
double[] delta = WGSToGCJPointer(latitude, longitude);
delta = GCJToBDPointer(delta[0], delta[1]);
String queryUrl = String.format("http://api.map.baidu.com/geocoder/v2/?ak=%s&location=%s,%s&output=json", baiduKeyIn, delta[0], delta[1]);
JsonObject object = JsonUtil.toJsonObject(FileUtil.stream2string(HttpUtil.httpGet(queryUrl).getEntity().getContent()));
String country = object.getAsJsonObject("result").getAsJsonObject("addressComponent").get("country").getAsString();
if (country.equals("中国")) {
country = "China";
}
if (country.equals("England")) {
country = "United Kingdom";
}
String countryCode = countryCodeByCountryName(country);
String countryChinese = countryChineseByCountryCode(countryCode);
String state = object.getAsJsonObject("result").getAsJsonObject("addressComponent").get("province").getAsString();
String stateCode = "";
if (country.equals("China")) {
stateCode = stateCodeByStateChinese(countryCode, state);
} else {
stateCode = stateCodeByStateName(countryCode, state);
}
state = stateNameByStateCode(countryCode, stateCode);
String stateChinese = stateChineseByStateCode(countryCode, stateCode);
String city = object.getAsJsonObject("result").getAsJsonObject("addressComponent").get("city").getAsString();
String description = object.getAsJsonObject("result").getAsJsonObject("addressComponent").get("district").getAsString();
countryCode = (countryCode == null) ? UNKNOWN : countryCode;
country = (country == null) ? UNKNOWN : country;
countryChinese = (countryChinese == null) ? UNKNOWN : countryChinese;
stateCode = (stateCode == null) ? UNKNOWN : stateCode;
state = (state == null) ? UNKNOWN : state;
stateChinese = (stateChinese == null) ? UNKNOWN : stateChinese;
city = (city == null) ? UNKNOWN : city;
return new GeoDef(latitude, longitude, countryCode, country, countryChinese, stateCode, state, stateChinese, city, description);
} catch (Exception e) {
LOGGER.error(Symbol.BLANK, e);
}
return null;
}
use of com.quickutil.platform.entity.GeoDef in project quickutil by quickutil.
the class GeoUtil method geoGpsByBaidu.
/**
* 根据经纬度查询地理信息-百度批量
*/
public static List<GeoDef> geoGpsByBaidu(List<GeoPoint> points) {
try {
if (points.size() > 20) {
return null;
}
StringBuilder builder = new StringBuilder();
for (int i = 0; i < points.size(); i++) {
double[] delta = WGSToGCJPointer(points.get(i).latitude, points.get(i).longitude);
delta = GCJToBDPointer(delta[0], delta[1]);
builder.append(delta[0] + "," + delta[1] + "|");
}
String queryUrl = String.format("http://api.map.baidu.com/geocoder/v2/?ak=%s&location=%s&output=json&batch=true", baiduKeyIn, URLEncoder.encode(builder.substring(0, builder.length() - 1), "UTF-8"));
JsonObject object = JsonUtil.toJsonObject(FileUtil.stream2string(HttpUtil.httpGet(queryUrl).getEntity().getContent()));
JsonArray array = object.getAsJsonArray("areas");
List<GeoDef> geodefList = new ArrayList<GeoDef>();
for (int i = 0; i < points.size(); i++) {
object = array.get(i).getAsJsonObject();
String country = object.get("country").getAsString();
if (country.equals("中国")) {
country = "China";
}
if (country.equals("England")) {
country = "United Kingdom";
}
String countryCode = countryCodeByCountryName(country);
String countryChinese = countryChineseByCountryCode(countryCode);
String state = object.get("province").getAsString();
String stateCode = "";
if (country.equals("China")) {
stateCode = stateCodeByStateChinese(countryCode, state);
} else {
stateCode = stateCodeByStateName(countryCode, state);
}
state = stateNameByStateCode(countryCode, stateCode);
String stateChinese = stateChineseByStateCode(countryCode, stateCode);
String city = object.get("city").getAsString();
String description = object.get("district").getAsString();
countryCode = (countryCode == null) ? UNKNOWN : countryCode;
country = (country == null) ? UNKNOWN : country;
countryChinese = (countryChinese == null) ? UNKNOWN : countryChinese;
stateCode = (stateCode == null) ? UNKNOWN : stateCode;
state = (state == null) ? UNKNOWN : state;
stateChinese = (stateChinese == null) ? UNKNOWN : stateChinese;
city = (city == null) ? UNKNOWN : city;
geodefList.add(new GeoDef(points.get(i).latitude, points.get(i).longitude, countryCode, country, countryChinese, stateCode, state, stateChinese, city, description));
}
return geodefList;
} catch (Exception e) {
LOGGER.error(Symbol.BLANK, e);
}
return null;
}
Aggregations