AMapUtils.java
4.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package com.glsx.common.utils.map;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.gavaghan.geodesy.Ellipsoid;
import org.gavaghan.geodesy.GeodeticCalculator;
import org.gavaghan.geodesy.GeodeticCurve;
import org.gavaghan.geodesy.GlobalCoordinates;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.glsx.common.constants.Constants;
import com.glsx.common.utils.HttpUtils;
import com.glsx.common.utils.PropertiesUtil;
/**
*
* @Title AMapUtils.java
* @Package com.glsx.common.utils.map
* @Description TODO 高德地图相关api
* @author zhanghj
* @date 2019年4月22日 下午5:03:01
* @version 1.0.0
*/
public class AMapUtils {
private static Logger logger = LoggerFactory.getLogger(AMapUtils.class);
public static final String GEOCODES_URL = "https://restapi.amap.com/v3/geocode/geo";
public static final String GEOCODES_REGEO_URL = "http://restapi.amap.com/v3/geocode/regeo";
public static Map<String, String> parseAddress(String address) {
Map<String, String> map = new HashMap<String, String>(4);
String key = PropertiesUtil.getProperty(Constants.AMAP_USER_KEY);
// String key = "4d8b3b5fc398f2f79f4329b5d07732fb";
Map<String, String> params = new HashMap<String, String>();
params.put("address", address);
params.put("key", key);
try {
String result = HttpUtils.get(GEOCODES_URL, params);
logger.info("获取高德行政编码返回结果:" + result);
if (StringUtils.isNotEmpty(result)) {
JSONObject json = JSON.parseObject(result);
if (json.containsKey("status")) {
int status = json.getIntValue("status");
if (status == 1) {
JSONArray geocodes = json.getJSONArray("geocodes");
JSONObject geocode = geocodes.getJSONObject(0);
map.put("adcode", geocode.getString("adcode"));
String location = geocode.getString("location");
map.put("longitude", location.substring(0, location.indexOf(",")));
map.put("latitude", location.substring(location.indexOf(",") + 1, location.length()));
}
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
logger.error("通过高德地图获取区域编码错误", e);
}
return map;
}
public static double getDistanceMeter(GlobalCoordinates gpsFrom, GlobalCoordinates gpsTo) {
// 创建GeodeticCalculator,调用计算方法,传入坐标系、经纬度用于计算距离
GeodeticCurve geoCurve = new GeodeticCalculator().calculateGeodeticCurve(Ellipsoid.Sphere, gpsFrom, gpsTo);
return geoCurve.getEllipsoidalDistance();
}
public static String getAddress(String latitude, String longitude) {
String key = PropertiesUtil.getProperty(Constants.AMAP_USER_KEY);
// String key = "4d8b3b5fc398f2f79f4329b5d07732fb";
Map<String, String> params = new HashMap<String, String>();
params.put("location", longitude + "," + latitude);
params.put("key", key);
try {
HttpUtils ht = new HttpUtils();
String result = ht.post(GEOCODES_REGEO_URL, params);
logger.info("获取高德定位地址返回结果:" + result);
if (StringUtils.isNotEmpty(result)) {
JSONObject json = JSON.parseObject(result);
if (json.containsKey("status")) {
int status = json.getIntValue("status");
if (status == 1) {
JSONObject regeocode = json.getJSONObject("regeocode");
String location = regeocode.getString("formatted_address");
return location;
}
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
logger.error("获取高德定位地址错误", e);
}
return null;
}
public static void main2(String[] args) {
// 西丽新屋村工业大厦
GlobalCoordinates source = new GlobalCoordinates(22.573316, 113.975012);
// 科兴科学园
GlobalCoordinates target = new GlobalCoordinates(22.554243, 113.950922);
double meter1 = getDistanceMeter(source, target);
// double meter2 = getDistanceMeter(source, target, Ellipsoid.WGS84);
// sphere坐标系与百度地图和高德地图较为接近
System.out.println("Sphere坐标系计算结果:" + meter1 + "米");
// System.out.println("WGS84坐标系计算结果:" + meter2 + "米");
}
public static void main(String[] args) {
String address = "广东省深圳市南山区科兴科学园C3栋";
Map<String, String> map = parseAddress(address);
String lat = "22.547834";
String lng = "113.943735";
System.out.println(map);
System.out.println(getAddress(lat, lng));
}
}