优化key获取

This commit is contained in:
Jian Shen
2022-07-18 16:56:27 +08:00
parent 6d611ff6b6
commit b86fb651ed
@@ -54,8 +54,10 @@ class GaoDeUtils {
* 获取高德KEY * 获取高德KEY
* @return * @return
*/ */
static String getGaodeKey() { static String getGaodeKey(String type) {
JSONObject json = HttpRequestUtil.getJsonContent("http://192.168.0.100:38080/FangJiaDataService/getAmapKey", new HashMap<String, String>()) Map<String, String> params = new HashMap<>();
params.put("type", type)
JSONObject json = HttpRequestUtil.getJsonContent("http://192.168.0.100:38080/FangJiaDataService/getAmapKey", params)
if (json.status == 0 && json.result != null) { if (json.status == 0 && json.result != null) {
return json.result return json.result
} }
@@ -74,7 +76,7 @@ class GaoDeUtils {
*/ */
static List searchNearByPoi(String city, String types, Double lng, Double lat, Integer radius, String keywords){ static List searchNearByPoi(String city, String types, Double lng, Double lat, Integer radius, String keywords){
def params = [:] def params = [:]
params.key = this.getGaodeKey()
if (types) { if (types) {
params.types = types params.types = types
@@ -91,8 +93,10 @@ class GaoDeUtils {
while (result.size() < count) { while (result.size() < count) {
params.page = "${page}".toString() params.page = "${page}".toString()
page++ page++
params.key = this.getGaodeKey("v3/place/around")
JSONObject json = HttpRequestUtil.getJsonContent("http://restapi.amap.com/v3/place/around", params); JSONObject json = HttpRequestUtil.getJsonContent("http://restapi.amap.com/v3/place/around", params);
if (json == null) { if (json == null) {
params.key = this.getGaodeKey("v3/place/around")
json = HttpRequestUtil.getJsonContent("http://restapi.amap.com/v3/place/around", params); json = HttpRequestUtil.getJsonContent("http://restapi.amap.com/v3/place/around", params);
} }
if (json && json.info && json.info == "OK") { if (json && json.info && json.info == "OK") {
@@ -126,7 +130,6 @@ class GaoDeUtils {
*/ */
static List searchNearDtoByPoi(String city, String types, Double lng, Double lat, Integer radius, String keywords){ static List searchNearDtoByPoi(String city, String types, Double lng, Double lat, Integer radius, String keywords){
def params = [:] def params = [:]
params.key = this.getGaodeKey()
if (types) { if (types) {
params.types = types params.types = types
@@ -143,8 +146,10 @@ class GaoDeUtils {
while (result.size() < count) { while (result.size() < count) {
params.page = "${page}".toString() params.page = "${page}".toString()
page++ page++
params.key = this.getGaodeKey("v5/place/around")
JSONObject json = HttpRequestUtil.getJsonContent("http://restapi.amap.com/v5/place/around", params); JSONObject json = HttpRequestUtil.getJsonContent("http://restapi.amap.com/v5/place/around", params);
if (json == null) { if (json == null) {
params.key = this.getGaodeKey("v5/place/around")
json = HttpRequestUtil.getJsonContent("http://restapi.amap.com/v5/place/around", params); json = HttpRequestUtil.getJsonContent("http://restapi.amap.com/v5/place/around", params);
} }
if (json && json.info && json.info == "OK") { if (json && json.info && json.info == "OK") {
@@ -211,11 +216,12 @@ class GaoDeUtils {
*/ */
static List<AmapPoi> getAmapById(id){ static List<AmapPoi> getAmapById(id){
def params = [:] def params = [:]
params.key = this.getGaodeKey()
params.id = id params.id = id
params.output = 'json' params.output = 'json'
params.key = this.getGaodeKey("v3/place/detail")
JSONObject json = HttpRequestUtil.getJsonContent("https://restapi.amap.com/v3/place/detail", params); JSONObject json = HttpRequestUtil.getJsonContent("https://restapi.amap.com/v3/place/detail", params);
if (json == null) { if (json == null) {
params.key = this.getGaodeKey("v3/place/detail")
json = HttpRequestUtil.getJsonContent("https://restapi.amap.com/v3/place/detail", params); json = HttpRequestUtil.getJsonContent("https://restapi.amap.com/v3/place/detail", params);
} }
if (json && json.info && json.info == "OK") { if (json && json.info && json.info == "OK") {
@@ -242,11 +248,12 @@ class GaoDeUtils {
*/ */
static List<AmapPoiDetail> getAmapDetailById(id){ static List<AmapPoiDetail> getAmapDetailById(id){
def params = [:] def params = [:]
params.key = this.getGaodeKey()
params.id = id params.id = id
params.output = 'json' params.output = 'json'
params.key = this.getGaodeKey("v3/place/detail")
JSONObject json = HttpRequestUtil.getJsonContent("https://restapi.amap.com/v3/place/detail", params); JSONObject json = HttpRequestUtil.getJsonContent("https://restapi.amap.com/v3/place/detail", params);
if (json == null) { if (json == null) {
params.key = this.getGaodeKey("v3/place/detail")
json = HttpRequestUtil.getJsonContent("https://restapi.amap.com/v3/place/detail", params); json = HttpRequestUtil.getJsonContent("https://restapi.amap.com/v3/place/detail", params);
} }
if (json && json.info && json.info == "OK") { if (json && json.info && json.info == "OK") {
@@ -273,7 +280,6 @@ class GaoDeUtils {
*/ */
static List searchPoi(String city, String keywords, String region, String typecodes){ static List searchPoi(String city, String keywords, String region, String typecodes){
def params = [:] def params = [:]
params.key = this.getGaodeKey()
params.keywords = keywords params.keywords = keywords
params.city = city params.city = city
params.region = region params.region = region
@@ -290,8 +296,10 @@ class GaoDeUtils {
while (result.size() < count) { while (result.size() < count) {
params.page = "${page}".toString() params.page = "${page}".toString()
page++ page++
params.key = this.getGaodeKey("v5/place/text")
JSONObject json = HttpRequestUtil.getJsonContent("http://restapi.amap.com/v5/place/text", params); JSONObject json = HttpRequestUtil.getJsonContent("http://restapi.amap.com/v5/place/text", params);
if (json == null) { if (json == null) {
params.key = this.getGaodeKey("v5/place/text")
json = HttpRequestUtil.getJsonContent("http://restapi.amap.com/v5/place/text", params); json = HttpRequestUtil.getJsonContent("http://restapi.amap.com/v5/place/text", params);
} }
if (json && json.info && json.info == "OK") { if (json && json.info && json.info == "OK") {
@@ -333,12 +341,13 @@ class GaoDeUtils {
*/ */
static def getGaoDeAddress(location) { static def getGaoDeAddress(location) {
Map<String, String> params = new HashMap<String, String>() Map<String, String> params = new HashMap<String, String>()
params.put("key", this.getGaodeKey())
params.put("location", location) params.put("location", location)
String content String content
try { try {
params.put("key", this.getGaodeKey("v3/geocode/regeo"))
content = new HttpRequest().get("http://restapi.amap.com/v3/geocode/regeo", params, "utf-8") content = new HttpRequest().get("http://restapi.amap.com/v3/geocode/regeo", params, "utf-8")
} catch (Exception e) { } catch (Exception e) {
params.put("key", this.getGaodeKey("v3/geocode/regeo"))
content = new HttpRequest().get("http://restapi.amap.com/v3/geocode/regeo", params, "utf-8") content = new HttpRequest().get("http://restapi.amap.com/v3/geocode/regeo", params, "utf-8")
} }
net.sf.json.JSONObject json = net.sf.json.JSONObject.fromObject(content) net.sf.json.JSONObject json = net.sf.json.JSONObject.fromObject(content)
@@ -365,13 +374,14 @@ class GaoDeUtils {
*/ */
static def parserByGaode(def city, def address) { static def parserByGaode(def city, def address) {
Map<String, String> params = new HashMap<String, String>() Map<String, String> params = new HashMap<String, String>()
params.put("key", this.getGaodeKey())
params.put("city", city) params.put("city", city)
params.put("address", address) params.put("address", address)
String content String content
try { try {
params.put("key", this.getGaodeKey("v3/geocode/geo"))
content = new HttpRequest().get("https://restapi.amap.com/v3/geocode/geo", params, "utf-8") content = new HttpRequest().get("https://restapi.amap.com/v3/geocode/geo", params, "utf-8")
} catch (Exception e) { } catch (Exception e) {
params.put("key", this.getGaodeKey("v3/geocode/geo"))
content = new HttpRequest().get("https://restapi.amap.com/v3/geocode/geo", params, "utf-8") content = new HttpRequest().get("https://restapi.amap.com/v3/geocode/geo", params, "utf-8")
} }
net.sf.json.JSONObject json = net.sf.json.JSONObject.fromObject(content) net.sf.json.JSONObject json = net.sf.json.JSONObject.fromObject(content)
@@ -393,4 +403,8 @@ class GaoDeUtils {
System.exit(1) System.exit(1)
} }
} }
static void main(String[] args) {
println(this.getGaodeKey("v3/geocode/geo"))
}
} }