无关风月
2025-01-16 aa0e37185b47cb59a8f90bcd81c2416ed0f24cfb
补充
3个文件已修改
114 ■■■■■ 已修改文件
DriverOKTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/GDMapGeocodingUtil.java 49 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ManagementOKTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/controller/general/TOpenCityController.java 6 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
ManagementOKTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/GaoDeMapUtil.java 59 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
DriverOKTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/util/GDMapGeocodingUtil.java
@@ -31,25 +31,26 @@
    /**
     * 将行政区域名称转化为坐标
     *
     * @param province
     * @param city
     * @param county
     * @param address
     * @return
     */
    public Map<String, Object> geocoding(String province, String city, String county, String address){
    public Map<String, Object> geocoding(String province, String city, String county, String address) {
        Map<String, Object> map = new HashMap<>();
        if(ToolUtil.isEmpty(province)){
        if (ToolUtil.isEmpty(province)) {
            map.put("status", -1);
            map.put("data", "省不能为空");
            return map;
        }
        if((ToolUtil.isEmpty(city) && ToolUtil.isNotEmpty(county)) || (ToolUtil.isEmpty(city) && ToolUtil.isNotEmpty(address))){
        if ((ToolUtil.isEmpty(city) && ToolUtil.isNotEmpty(county)) || (ToolUtil.isEmpty(city) && ToolUtil.isNotEmpty(address))) {
            map.put("status", -1);
            map.put("data", "市不能为空");
            return map;
        }
        if((ToolUtil.isEmpty(county) && ToolUtil.isNotEmpty(address))){
        if ((ToolUtil.isEmpty(county) && ToolUtil.isNotEmpty(address))) {
            map.put("status", -1);
            map.put("data", "县/区不能为空");
            return map;
@@ -64,9 +65,9 @@
//        gdInterfaceService.saveData("https://restapi.amap.com/v3/geocode/geo", "行政区域转经纬度");
        if(status.equals("1")){
        if (status.equals("1")) {
            JSONArray geocodes = jsonObject.getJSONArray("geocodes");
            for(int i = 0; i < geocodes.size(); i++){
            for (int i = 0; i < geocodes.size(); i++) {
                String location = geocodes.getJSONObject(i).getString("location");
                list.add(location);
            }
@@ -77,25 +78,42 @@
    }
    public Map<String, Object> geocoding(String address) {
        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");
        List<String> list = new ArrayList<>();
        if (status.equals("0")) {
            JSONObject result = jsonObject.getJSONObject("result");
            JSONObject location = result.getJSONObject("location");
            String lng = location.getString("lng");
            String lat = location.getString("lat");
            list.add(lat + "," + lng);
        }
        map.put("status", 0);
        map.put("data", list);
        return map;
    }
    /**
     * 根据经纬度获取行政区域信息
     *
     * @param lon
     * @param lan
     * @return
     * @throws Exception
     */
    public Map<String, String> geocode(String lon, String lan) throws Exception{
        String url = "https://restapi.amap.com/v3/geocode/regeo?key=" + key + "&location=" + lon + "," + lan;
    public Map<String, String> geocode(String lon, String lan) throws Exception {
        String url = "https://api.map.baidu.com/reverse_geocoding/v3/?ak=" + key + "&output=json&coordtype=bd09ll" + "&location=" + lan + "," + lon;
        String forObject = restTemplate.getForObject(url, String.class);
        JSONObject jsonObject = JSON.parseObject(forObject);
        Map<String, String> map = new HashMap<>();
//        gdInterfaceService.saveData("https://restapi.amap.com/v3/geocode/regeo", "经纬度转行政区域");
        if(jsonObject.getString("status").equals("1")){
            JSONObject regeocode = jsonObject.getJSONObject("regeocode");
            JSONObject addressComponent = regeocode.getJSONObject("addressComponent");
            String address = regeocode.getString("formatted_address");
        if (jsonObject.getString("status").equals("0")) {
            JSONObject result = jsonObject.getJSONObject("result");
            JSONObject addressComponent = result.getJSONObject("addressComponent");
            String address = result.getString("formatted_address");
            map.put("address", address);
            String code = addressComponent.getString("adcode");
            String province = addressComponent.getString("province");
@@ -111,3 +129,4 @@
        return map;
    }
}
ManagementOKTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/controller/general/TOpenCityController.java
@@ -122,6 +122,8 @@
        return super.packForBT(page);
    }
    @Autowired
    private  GaoDeMapUtil gaoDeMapUtil;
    /**
     * 新增城市管理
     */
@@ -152,7 +154,7 @@
        if (SinataUtil.isNotEmpty(tOpenCity.getProvinceName())){
            address+=tOpenCity.getProvinceName();
        }
        JSONObject positionObj = GaoDeMapUtil.getLngAndLat(address);
        JSONObject positionObj = gaoDeMapUtil.getLngAndLat(address);
        String longitude = positionObj.getString("longitude");
        String latitude = positionObj.getString("latitude");
        tOpenCity.setLon(Double.valueOf(longitude));
@@ -264,7 +266,7 @@
        if (SinataUtil.isNotEmpty(tOpenCity.getProvinceName())){
            address+=tOpenCity.getProvinceName();
        }
        JSONObject positionObj = GaoDeMapUtil.getLngAndLat(address);
        JSONObject positionObj = gaoDeMapUtil.getLngAndLat(address);
        String longitude = positionObj.getString("longitude");
        String latitude = positionObj.getString("latitude");
        tOpenCity.setLon(Double.valueOf(longitude));
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;
    }