package com.zy.sc.common.service;
|
|
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSONObject;
|
import com.core.common.Arith;
|
import com.core.common.Cools;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.stereotype.Service;
|
import com.zy.sc.common.entity.geo.GeoCodeDto;
|
import com.zy.sc.manager.utils.HttpHandler;
|
|
/**
|
* Created by vincent on 2021/12/21
|
*/
|
@Slf4j
|
@Service
|
public class GeoService {
|
|
private static final String GEO_WEB_CODE_URL = "https://restapi.amap.com/v3/geocode/regeo";
|
private static final String GEO_WEB_KEY = "c3335c3c57d7cd4a276a890eeb72c1e9";
|
|
public GeoCodeDto getGeoCode(Double lon, Double lat) {
|
try {
|
String param = Arith.multiplys(2, lon, 1) + "," + Arith.multiplys(2, lat, 1);
|
String response = new HttpHandler.Builder()
|
.setUri(GEO_WEB_CODE_URL)
|
.setParams(Cools.add("key", GEO_WEB_KEY).add("location", param))
|
.build()
|
.doGet();
|
JSONObject jsonObject = JSON.parseObject(response);
|
if (jsonObject.getString("infocode").equals("10000")) {
|
return JSON.parseObject(jsonObject.getString("regeocode"), GeoCodeDto.class);
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
log.error("geo fail", e);
|
}
|
return null;
|
}
|
|
public static void main(String[] args) {
|
new GeoService().getGeoCode(120.894321000, 30.944699000);
|
}
|
|
|
}
|