地理/逆地理编码

1.高德

官网:https://lbs.amap.com/api/webservice/guide/api/georegeo

(1)工具类

package com.aibe.common;

import com.aibe.util.http.HttpResult;
import com.aibe.util.http.HttpclientUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class GaoDeUtils {
    private static final String key="";
    private static final String url="https://restapi.amap.com/v3/geocode/geo";

    public static String getLatLon(String address){
        Map<String,Object> parameters=new HashMap<>();
        parameters.put("key",key);
        parameters.put("address",address);
        try {
            HttpResult result = HttpclientUtils.doGet(url, parameters);
            if (result.getCode()==200){
                String body = result.getBody();
                JSONObject j= JSON.parseObject(body);
                String info = j.getString("info");
                if (info.equals("OK")){
                    JSONArray geocodes = j.getJSONArray("geocodes");
                    for (int i = 0; i < geocodes.size(); i++) {
                        JSONObject jsonObject = geocodes.getJSONObject(i);
                        return jsonObject.getString("location");
                    }
                }
                return info;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return null;
    }

    public static void main(String[] args) {
        String ss = getLatLon("贵州省贵阳市南明区公园中路花果园M区");
        System.out.println(ss);
    }
}

(2) 请求http工具

package com.aibe.util.http;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ocean
 * @date 2022/11/17 0017 9:49
 */
public class HttpclientUtils {

    //1.不带参数的get请求的方法
    /**
     *
     * @param url
     * @return
     * @throws Exception
     */
    public static HttpResult doGet(String url) throws Exception{
        return doGet(url,null);
    }



    //2.带参数的get请求的方法
    /**
     *
     * @param url  请求的URL
     * @param map  请求传递的参数
     * @return
     * @throws Exception
     */
    public static HttpResult doGet(String url,Map<String, Object> map) throws Exception{

        //1.创建httpclient的对象
        CloseableHttpClient client = HttpClients.createDefault();
        //2.创建httpget get请求对象
        URIBuilder builder =  new URIBuilder(url);//设置URL
        //遍历参数,设置参数的值
        if(map!=null){
            for (Map.Entry<String, Object> entry : map.entrySet()) {

                builder.setParameter(entry.getKey(), entry.getValue().toString());
            }
        }
        URI uri = builder.build();
        HttpGet httpGet = new HttpGet(uri);
        //3.执行请求
        CloseableHttpResponse response = client.execute(httpGet);
        //4.获取响应的结果 封装到httpresult中
        Integer code = response.getStatusLine().getStatusCode();//状态码
        String body =null;
        if(response.getEntity()!=null){
            body = EntityUtils.toString(response.getEntity(), "utf-8");
        }
        HttpResult result = new HttpResult(code, body);
        return result;
    }


    //3.不带参数的post请求的方法
    public static HttpResult doPost(String url) throws Exception{
        return doPost(url,null);
    }
    //4.带参数的post请的方法
    public static HttpResult doPost(String url,Map<String, Object> map) throws Exception{
        //1. 创建httpclient 对象
        CloseableHttpClient client = HttpClients.createDefault();

        //2.创建httppost 请求对象

        HttpPost httpPost = new HttpPost(url);

        if(map!=null){
            //遍历参数的map集合  设置参数列表
            List<NameValuePair> parameters = new ArrayList<>();

            for (Map.Entry<String, Object> entry : map.entrySet()) {
                parameters.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString()));
            }
            //创建表单实体对象,将参数设置到表单实体中
            UrlEncodedFormEntity encodedFormEntity = new UrlEncodedFormEntity(parameters);

            //设置表单实体到httpost请求对象中
            httpPost.setEntity(encodedFormEntity);
        }

        //3.执行请求
        CloseableHttpResponse response = client.execute(httpPost);
        //4.获取响应结果,封装到httpresult中
        Integer code = response.getStatusLine().getStatusCode();//状态码
        String body = null;
        if(response.getEntity()!=null){
            body =  EntityUtils.toString(response.getEntity(), "utf-8");
        }
        //返回httpresult
        HttpResult result = new HttpResult(code, body);
        return result;
    }
    //5.带参数和header的post请的方法
    public static HttpResult doPost(String url,JSONObject jsonParam,Map<String, Object> headers) throws Exception{
        //1. 创建httpclient 对象
        CloseableHttpClient client = HttpClients.createDefault();

        //2.创建httppost 请求对象

        HttpPost httpPost = new HttpPost(url);

        StringEntity entity = new StringEntity(jsonParam.toString(), "utf-8");
        entity.setContentEncoding("UTF-8");
        entity.setContentType("application/json");
        httpPost.setEntity(entity);

        if(headers!=null){
            //遍历参数的headers集合  设置请求头列表
            for (Map.Entry<String, Object> entry : headers.entrySet()) {
                httpPost.addHeader(entry.getKey(), entry.getValue().toString());
            }
        }


        //3.执行请求
        CloseableHttpResponse response = client.execute(httpPost);
        //4.获取响应结果,封装到httpresult中
        Integer code = response.getStatusLine().getStatusCode();//状态码
        String body = null;
        if(response.getEntity()!=null){
            body =  EntityUtils.toString(response.getEntity(), "utf-8");
        }
        //返回httpresult
        HttpResult result = new HttpResult(code, body);
        return result;
    }

    //6.带参数和header的post请的方法
    public static HttpResult doPost(String url,Map<String, Object> map,Map<String, Object> headers,boolean b) throws Exception{
        //1. 创建httpclient 对象
        CloseableHttpClient client = HttpClients.createDefault();

        //2.创建httppost 请求对象

        HttpPost httpPost = new HttpPost(url);

        if(map!=null){
            //遍历参数的map集合  设置参数列表
            List<NameValuePair> parameters = new ArrayList<>();

            for (Map.Entry<String, Object> entry : map.entrySet()) {
                parameters.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString()));
            }
            //创建表单实体对象,将参数设置到表单实体中
            UrlEncodedFormEntity encodedFormEntity = new UrlEncodedFormEntity(parameters);

            //设置表单实体到httpost请求对象中
            httpPost.setEntity(encodedFormEntity);
        }




        if(headers!=null){
            //遍历参数的headers集合  设置请求头列表
            for (Map.Entry<String, Object> entry : headers.entrySet()) {
                httpPost.addHeader(entry.getKey(), entry.getValue().toString());
            }
        }


        //3.执行请求
        CloseableHttpResponse response = client.execute(httpPost);
        //4.获取响应结果,封装到httpresult中
        Integer code = response.getStatusLine().getStatusCode();//状态码
        String body = null;
        if(response.getEntity()!=null){
            body =  EntityUtils.toString(response.getEntity(), "utf-8");
        }
        //返回httpresult
        HttpResult result = new HttpResult(code, body);
        return result;
    }



}

package com.aibe.util.http;


/**
 * @author ocean
 * @date 2022/11/17 0017 9:53
 */
public class HttpResult {
    private Integer code;//响应的状态码 200 201.。
    private String body;//响应体 (响应的内容)
    public Integer getCode() {
        return code;
    }
    public void setCode(Integer code) {
        this.code = code;
    }
    public String getBody() {
        return body;
    }
    public void setBody(String body) {
        this.body = body;
    }
    public HttpResult(Integer code, String body) {
        super();
        this.code = code;
        this.body = body;
    }

    @Override
    public String toString() {
        return "HttpResult{" +
                "code=" + code +
                ", body='" + body + '\'' +
                '}';
    }

    public HttpResult() {

    }

}


  • 9
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值