高德地图获取经纬度工具类
MapUtils
public class MapUtils {
public static String getHttpResponse(String serverUrl) {
BufferedReader bf = null;
StringBuffer result = null;
try {
//构造一个URI
URI uri = new URI(serverUrl);
//根据URI构造一个URL
URL url = uri.toURL();
//返回一个 URLConnection 对象,它表示到 URL 所引用的远程对象的连接
URLConnection connection = url.openConnection();
//设置请求属性
connection.setRequestProperty("Content-type", "text/html");
connection.setRequestProperty("Accept-Charset", "utf-8");
connection.setRequestProperty("ContentType", "utf-8");
connection.connect();
result = new StringBuffer();
//读取打开的连接的输入流并放入到缓冲区中
bf = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
String line;
while ((line = bf.readLine()) != null) {
result.append(line);
}
return result.toString();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (bf != null) {
bf.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return null;
}
public static String distance(String origin, String destination) {
String url = "http://restapi.amap.com/v4/direction/truck?"
+ "&size=" + 4
+ "&origin=" + origin
+ "&destination=" + destination
+ "&key=" + "5ccc58bc0e4447bb60a192ad5146055c";
String distanceString = getHttpResponse(url);
//JSONObject json = JSONObject.parseObject(distanceString);
return distanceString;
}
/**
* 高德地图通过地址获取经纬度
*/
public static Map<String, Double> addressToLngAndLag(String address) {
Map<String, Double> map = new HashMap<String, Double>();
//"http://restapi.amap.com/v3/geocode/geo?address=上海市东方明珠&output=JSON&key=xxxxxxxxx";
String geturl = "http://restapi.amap.com/v3/geocode/geo?key=5ccc58bc0e4447bb60a192ad5146055c&address=" + address;
String location = "";
try {
URL url = new URL(geturl); // 把字符串转换为URL请求地址
HttpURLConnection connection = (HttpURLConnection) url.openConnection();// 打开连接
connection.connect();// 连接会话
// 获取输入流
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
StringBuilder sb = new StringBuilder();
while ((line = br.readLine()) != null) {// 循环读取流
sb.append(line);
}
br.close();// 关闭流
connection.disconnect();// 断开连接
com.alibaba.fastjson.JSONObject a = JSON.parseObject(sb.toString());
JSONArray sddressArr = JSON.parseArray(a.get("geocodes").toString());
if (sddressArr.size() == 0) return null;
JSONObject c = JSON.parseObject(sddressArr.get(0).toString());
location = c.get("location").toString();
} catch (Exception e) {
e.printStackTrace();
System.out.println("失败!");
}
return stringToMap(location);
}
public static Map<String, Double> stringToMap(String LngLat) {
Map<String, Double> map = new HashMap<String, Double>();
String[] strArr = LngLat.split("\\,");
map.put("lng", Double.parseDouble(strArr[0]));
map.put("lat", Double.parseDouble(strArr[1]));
return map;
}
}
测试demo
@Test
public void e() {
Map<String, Double> queryString = MapUtils.addressToLngAndLag("平阳居公寓");
Double lng = queryString.get("lng");//纬度
Double lat = queryString.get("lat");//精度
System.out.println(lat);//27.632457
System.out.println(lng);//120.318786
}