8 changed files with 158 additions and 14 deletions
@ -0,0 +1,81 @@
|
||||
package org.springblade.common.utils; |
||||
|
||||
import com.alibaba.fastjson.JSON; |
||||
import com.alibaba.fastjson.JSONObject; |
||||
|
||||
import java.io.BufferedReader; |
||||
import java.io.InputStreamReader; |
||||
import java.math.BigDecimal; |
||||
import java.net.HttpURLConnection; |
||||
import java.net.URL; |
||||
import java.net.URLConnection; |
||||
import java.net.URLEncoder; |
||||
import java.util.Arrays; |
||||
import java.util.HashMap; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
public class AddressUtil { |
||||
|
||||
|
||||
// private static String KEY="<高德key>";
|
||||
|
||||
|
||||
// public static String GD_URL="http://restapi.amap.com/v3/geocode/geo?key=<高德地图KEY>&address=";
|
||||
public static String GD_URL="http://restapi.amap.com/v3/geocode/geo?key=af745167b52e6548d450cbbf2c6134eb&address="; |
||||
|
||||
public static String SUCCESS="10000"; |
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/** |
||||
* @param addressName |
||||
* @Description 根据高德地图, 通过地址获取经纬度坐标 |
||||
* @Throws |
||||
* @Return java.lang.String |
||||
* @Date 2023-04-13 10:50:22 |
||||
* @Author WangKun |
||||
*/ |
||||
public static Map<String, BigDecimal> getLatAndLngOrGDMap(String addressName) { |
||||
try { |
||||
Map<String, BigDecimal> map = new HashMap<>(); |
||||
String address = URLEncoder.encode(addressName, "UTF-8"); |
||||
String mapUrl = GD_URL + addressName ; |
||||
URL url = new URL(mapUrl); |
||||
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(); |
||||
JSONObject a = JSON.parseObject(sb.toString()); |
||||
if (!SUCCESS.equals(a.get("infocode"))){ |
||||
//解析不成功
|
||||
return null; |
||||
} |
||||
JSONObject c = JSON.parseObject(JSON.parseArray(a.get("geocodes").toString()).get(0).toString()); |
||||
String location = c.get("location").toString(); |
||||
List<String> result = Arrays.asList(location.split(",")); |
||||
map.put("lng", new BigDecimal(result.get(0))); |
||||
map.put("lat", new BigDecimal(result.get(1))); |
||||
return map; |
||||
} catch (IndexOutOfBoundsException index) { |
||||
throw new IndexOutOfBoundsException(); |
||||
} catch (Exception e) { |
||||
e.printStackTrace(); |
||||
System.out.println("失败!"); |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
|
||||
} |
Loading…
Reference in new issue