通过经纬度获取区域信息

今天公司下发任务,其中有个需求,就是通过经纬度获取区域相关信息,然后自己在网上找了大量资料,并尝试都没有成功,所以自己决定写一个工具类,工具类已写好,代码如下:

package com.cxy.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;

public class BaiduUtil {
    /**
     * 根据经纬度获取区域信息
     * @param lng 经度
     * @param lat 纬度
     * @return
     * @throws IOException
     */
    public static Map<String,Object> getAreaInfo(Double lng, Double lat) throws IOException {
        StringBuilder resultData = new StringBuilder();
        String url ="http://api.map.baidu.com/geocoder?location=" + lat + ","+ lng + "&output=json&pois=1";
        URL myURL = null;
        URLConnection httpsConn = null;

        try {
            myURL = new URL(url);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

        InputStreamReader isr = null;
        BufferedReader br = null;

        try {
            httpsConn = (URLConnection) myURL.openConnection();// 不使用代理
            if (httpsConn != null) {
                isr = new InputStreamReader( httpsConn.getInputStream(), "UTF-8");
                br = new BufferedReader(isr);
                String data = null;
                while((data = br.readLine()) != null){
                    resultData.append(data);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(isr != null){
                isr.close();
            }
            if(br != null){
                br.close();
            }
        }

        //可以根据自己的需求自定义获取
        JSONObject addressComponent = JSON.parseObject(resultData.toString()).getJSONObject("result").getJSONObject("addressComponent");

        Map<String,Object> area = new HashMap<>();
        area.put("city",addressComponent.get("city"));
        area.put("direction",addressComponent.get("direction"));
        area.put("distance",addressComponent.get("distance"));
        area.put("district",addressComponent.get("district"));
        area.put("province",addressComponent.get("province"));
        area.put("street",addressComponent.get("street"));
        area.put("street_number",addressComponent.get("street_number"));
        
        return area;
    }

    public static void main(String[] args) throws IOException {
    	//演示
        Map<String, Object> areaInfo = getAreaInfo(121.487899486, 31.24916171);
        System.out.println(areaInfo .get("province"));
    }
}

备注

  • 所需依赖
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.62</version>
</dependency>
  • 百度api返回json格式
// 20200915210046
// http://api.map.baidu.com/geocoder?location=31.24916171,121.487899486&output=json&pois=1

{
  "status": "OK",
  "result": {
    "location": {
      "lng": 121.487899,
      "lat": 31.249162
    },
    "formatted_address": "上海市闸北区天潼路619号",
    "business": "七浦路,海宁路,北京东路",
    "addressComponent": {
      "city": "上海市",
      "direction": "附近",
      "distance": "32",
      "district": "闸北区",
      "province": "上海市",
      "street": "天潼路",
      "street_number": "619号"
    },
    "cityCode": 289
  }
}

版权声明:本文为qq_46018521原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。