无关风月
2025-01-16 aa0e37185b47cb59a8f90bcd81c2416ed0f24cfb
ManagementOKTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/GaoDeMapUtil.java
@@ -1,4 +1,5 @@
package com.stylefeng.guns.modular.system.util;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
@@ -8,21 +9,23 @@
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Component
public class GaoDeMapUtil {
    public static void main(String[] args) {
        // 地址名称
        String address = "深圳市";
        // 调用自己写好的封装方法
        JSONObject positionObj = getLngAndLat(address);
        String longitude = positionObj.getString("longitude");
        String latitude = positionObj.getString("latitude");
        System.out.println("经度:" + longitude);
        System.out.println("纬度:" + latitude);
    }
    @Autowired
    private RestTemplate restTemplate;
    private String key = "WQhfsluNzEeUHUxoH4jc4JiCQOXw4Mnx";
    /**
     * 根据地址查询经纬度
@@ -30,30 +33,20 @@
     * @param address
     * @return
     */
    public static JSONObject getLngAndLat(String address) {
    public  JSONObject getLngAndLat(String address) {
        JSONObject positionObj = new JSONObject();
        try {
            // 拼接请求高德的url
            String url = "http://restapi.amap.com/v3/geocode/geo?address=" + address + "&output=JSON&key=" + "WQhfsluNzEeUHUxoH4jc4JiCQOXw4Mnx";
            // 请求高德接口
            String result = sendHttpGet(url);
            JSONObject resultJOSN = JSONObject.parseObject(result);
            System.out.println("高德接口返回原始数据:");
            System.out.println(resultJOSN);
            JSONArray geocodesArray = resultJOSN.getJSONArray("geocodes");
            if (geocodesArray.size() > 0) {
                String position = geocodesArray.getJSONObject(0).getString("location");
                String[] lngAndLat = position.split(",");
                String longitude = lngAndLat[0];
                String latitude = lngAndLat[1];
                positionObj.put("longitude", longitude);
                positionObj.put("latitude", latitude);
            }
            geocodesArray.getJSONObject(0).getString("location");
        } catch (Exception e) {
            e.printStackTrace();
        Map<String, Object> map = new HashMap<>();
        String url = "https://api.map.baidu.com/geocoding/v3/?address" + address + "&output=json&ak=" + key;
        String forObject = restTemplate.getForObject(url, String.class);
        JSONObject jsonObject = JSON.parseObject(forObject);
        String status = jsonObject.getString("status");
        if (status.equals("0")) {
            JSONObject result = jsonObject.getJSONObject("result");
            JSONObject location = result.getJSONObject("location");
            String lng = location.getString("lng");
            String lat = location.getString("lat");
            positionObj.put("longitude", lng);
            positionObj.put("latitude", lat);
        }
        return positionObj;
    }