浏览代码

init: init project step4: biz code support

shc 1 年之前
父节点
当前提交
bb1267d65a
共有 1 个文件被更改,包括 9 次插入20 次删除
  1. 9 20
      hnqz-upms/hnqz-upms-biz/src/main/java/com/qunzhixinxi/hnqz/admin/util/MapUtil.java

+ 9 - 20
hnqz-upms/hnqz-upms-biz/src/main/java/com/qunzhixinxi/hnqz/admin/util/MapUtil.java

@@ -2,23 +2,19 @@ package com.qunzhixinxi.hnqz.admin.util;
 
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 import java.io.*;
 import java.net.HttpURLConnection;
 import java.net.URL;
-import java.net.URLConnection;
-import java.util.List;
-import java.util.Map;
-import java.util.regex.Pattern;
+import java.nio.charset.StandardCharsets;
 
 import static com.qunzhixinxi.hnqz.admin.util.HttpRequest.sendGet;
 import static com.qunzhixinxi.hnqz.admin.util.HttpRequest.sendPost;
 
 public class MapUtil {
 
-	private static String key = "8ef7f492add60ae866606e6928ed2576";
+	private static final String key = "8ef7f492add60ae866606e6928ed2576";
+	private static final String API_URL = "https://restapi.amap.com/v3/geocode/regeo";
 
 	/**
 	 * 阿里云api 根据经纬度获取地址
@@ -30,7 +26,7 @@ public class MapUtil {
 	public static String getAdd(String log, String lat) {
 		StringBuffer s = new StringBuffer();
 		s.append("key=").append(key).append("&location=").append(log).append(",").append(lat);
-		String res = sendPost("http://restapi.amap.com/v3/geocode/regeo", s.toString());
+		String res = sendPost(API_URL, s.toString());
 		System.out.print(res);
 		JSONObject jsonObject = JSONObject.parseObject(res);
 		JSONObject jsonObject1 = jsonObject.getJSONObject("regeocode");
@@ -56,7 +52,7 @@ public class MapUtil {
 			conn.setDoOutput(true);
 			conn.setRequestMethod("POST");
 			java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(conn.getInputStream
-					(), "UTF-8"));
+					(), StandardCharsets.UTF_8));
 			String line;
 			while ((line = in.readLine()) != null) {
 				res += line + "\n";
@@ -66,7 +62,7 @@ public class MapUtil {
 			JSONArray jsonArray = JSONArray.parseArray(jsonObject.getString("addrList"));
 			JSONObject j_2 = JSONObject.parseObject(jsonArray.get(0).toString());
 			String allAdd = j_2.getString("admName");
-			String arr[] = allAdd.split(",");
+			String[] arr = allAdd.split(",");
 			res = arr[1];
 		} catch (Exception e) {
 			System.out.print("error in wapaction,and e is " + e.getMessage());
@@ -75,6 +71,7 @@ public class MapUtil {
 		return res;
 	}
 
+
 	/**
 	 * 高德api 根据地址获取经纬度
 	 *
@@ -84,7 +81,7 @@ public class MapUtil {
 	public static JSONObject getLatAndLogByName(String name) {
 		StringBuffer s = new StringBuffer();
 		s.append("key=" + key + "&address=" + name);
-		String res = sendPost("http://restapi.amap.com/v3/geocode/geo", s.toString());
+		String res = sendPost("https://restapi.amap.com/v3/geocode/geo", s.toString());
 		JSONObject jsonObject = JSONObject.parseObject(res);
 		JSONArray jsonArray = JSONArray.parseArray(jsonObject.getString("geocodes"));
 		if(null==jsonArray||jsonArray.size()==0)
@@ -99,13 +96,12 @@ public class MapUtil {
 	/**
 	 * 高德api 根据地址获取经纬度
 	 *
-	 * @param name
 	 * @return
 	 */
 	public static String getAddByAMAP(String log, String lat) {
 		StringBuffer s = new StringBuffer();
 		s.append("key=").append(key).append("&location=").append(log).append(",").append(lat);
-		String res = sendPost("http://restapi.amap.com/v3/geocode/regeo", s.toString());
+		String res = sendPost(API_URL, s.toString());
 		JSONObject jsonObject = JSONObject.parseObject(res);
 		JSONObject jsonObject1 = jsonObject.getJSONObject("regeocode");
 		String add = jsonObject1.get("formatted_address").toString();
@@ -116,7 +112,6 @@ public class MapUtil {
 	/**
 	 * 高德api 坐标转换---转换至高德经纬度
 	 *
-	 * @param name
 	 * @return
 	 */
 	public static String convertLocations(String log, String lat, String type) {
@@ -216,10 +211,4 @@ public class MapUtil {
 		System.out.print(around);
 		return around;
 	}
-//
-//	public static void main(String args [] )
-//	{
-//		JSONObject jsonObject=getLatAndLogByName("绩溪县北大街82号");
-//		System.out.println(jsonObject.toJSONString());
-//	}
 }