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