#
luxiaotao1123
2022-03-01 5e4bef66a37f1bbdf48d845dda518fd8cc54ef37
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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);
    }
 
 
}