package com.cy.test;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
public class XYUtils {
// 高德地图key
private static final String GDMAP_KEY = "高德地图密钥";
// 高德地图地理编码 API 服务地址
private static final String gdUrl = "https://restapi.amap.com/v3/geocode/geo?parameters";
// 高德地图逆地理编码API服务地址
private static final String reverseGeocodingGdUrl = "https://restapi.amap.com/v3/geocode/regeo?parameters";
// 百度地图AK
private static final String BDMAP_AK = "百度地图密钥";
// 百度地图地理编码 API 服务地址
private static final String geocodingBdUrl = "http://api.map.baidu.com/geocoding/v3/?output=json";
// 百度地图逆地理编码 API 服务地址
private static final String reverseGeocodingBdUrl = "http://api.map.baidu.com/reverse_geocoding/v3/?output=json&coordtype=wgs84ll";
// 百度地图获取地址的经纬度
public static Map<String, String> getXYByBaiduMap(String address) {
Map<String, String> xyMap = new HashMap<>();
xyMap.put("status", "0"); // 初始化
Map<String, String> params = new HashMap<>();
params.put("address", address);
params.put("ak", BDMAP_AK);
// 调用百度地图API获取地址经纬度,结果例:showLocation&&showLocation({"status":0,"result":{"location":{"lng":117.07158975107194,"lat":36.68565553471069},"precise":0,"confidence":20,"comprehension":100,"level":"区县"}})
String result = HttpClientUtils.doGet(geocodingBdUrl, params, null);
// 将结果转换,方便取值
JSONObject jsonObject = JSONObject.parseObject(result);
// status 为0时为正常,详情见http://lbsyun.baidu.com/index.php?title=webapi/guide/webservice-geocoding
if ("0".equals(jsonObject.getString("status"))) {
JSONObject resultObj = JSONObject.parseObject(jsonObject.getString("result")); // 结果集
JSONObject locationObj = JSONObject.parseObject(resultObj.getString("location")); // 地址信息
String longitude = locationObj.getString("lng"); // 经度
String latitude = locationObj.getString("lat"); // 纬度
// 将经纬度存入map中,返回数据
xyMap.put("latitude", latitude);
xyMap.put("longitude", longitude);
xyMap.put("status", "1");
}
return xyMap;
}
// 根据经纬度返回具体地址.(纬度,经度) 百度
public static Map<String,String> getAddressByXYByBaiduMap(String lnglat) {
// 初始化请求参数
Map<String, String> params = new HashMap<>();
// 初始化结果集
Map<String,String> resultMap = new LinkedHashMap<>();
// 请求参数
params.put("location", lnglat);
params.put("ak", BDMAP_AK);
// 发送请求,返回结果
String resultStr = HttpClientUtils.doGet(reverseGeocodingBdUrl, params, null);
JSONObject jsonObject = JSONObject.parseObject(resultStr);
if("0".equals(jsonObject.getString("status"))){
JSONObject resultObject = JSONObject.parseObject(jsonObject.getString("result"));
resultMap.put("address",resultObject.getString("formatted_address")); //
resultMap.put("business",resultObject.getString("business")); // 所属商圈
JSONObject addressObject = JSONObject.parseObject(resultObject.getString("addressComponent"));
resultMap.put("country",addressObject.getString("country"));
resultMap.put("province",addressObject.getString("province"));
resultMap.put("city",addressObject.getString("city"));
resultMap.put("district",addressObject.getString("district"));
resultMap.put("town",addressObject.getString("town"));
resultMap.put("street",addressObject.getString("street"));
}
return resultMap;
}
/* *************************************************************************************************** */
// 高德地图获取地址的经纬度
public static Map<String, String> getXYByGaodeMap(String address) {
Map<String, String> xyMap = new HashMap<>();
Map<String, String> params = new HashMap<>();
params.put("key", GDMAP_KEY);
params.put("address", address);
// 调用高德地图API获取地址结果集
String result = HttpClientUtils.doGet(gdUrl, params, null);
// 将结果转换,方便取值
JSONObject jsonObject = JSONObject.parseObject(result);
// status 为1表示请求成功
if ("1".equals(jsonObject.getString("status"))) {
JSONArray resultArr = JSONArray.parseArray(jsonObject.getString("geocodes")); // 结果集
Map<String, String> resultMap = (Map<String, String>) resultArr.get(0); // 返回是数组类型,转换
String lnglat = resultMap.get("location"); // 获取经纬度
// 将经纬度存入map中,返回数据. {"location":"经度,纬度"}
xyMap.put("longitude", lnglat.split(",")[0]);
xyMap.put("latitude", lnglat.split(",")[1]);
}
xyMap.put("status", jsonObject.getString("status"));
return xyMap;
}
// 根据经纬度返回具体地址.(纬度,经度)
public static Map<String,String> getAddressByXYByGaodeMap(String lngLat) {
// 初始化请求参数
Map<String, String> params = new HashMap<>();
// 初始化结果集
Map<String,String> resultMap = new LinkedHashMap<>();
// 请求参数
params.put("location", lngLat);
params.put("key", GDMAP_KEY);
// 发送请求,返回结果
String resultStr = HttpClientUtils.doGet(reverseGeocodingGdUrl, params, null);
JSONObject jsonObject = JSONObject.parseObject(resultStr);
if("1".equals(jsonObject.get("status"))){
JSONObject resultObject = JSONObject.parseObject(jsonObject.getString("regeocode"));
resultMap.put("address",resultObject.getString("formatted_address"));
JSONObject addressComponentObject = JSONObject.parseObject(resultObject.getString("addressComponent"));
resultMap.put("country",addressComponentObject.getString("country"));
resultMap.put("province",addressComponentObject.getString("province"));
resultMap.put("city",addressComponentObject.getString("city"));
resultMap.put("district",addressComponentObject.getString("district"));
resultMap.put("township",addressComponentObject.getString("township"));
resultMap.put("building",addressComponentObject.getString("building"));
}
return resultMap;
}
}
XYUtils —— 获取经纬度工具类
最新推荐文章于 2023-08-20 01:04:02 发布