使用百度api 获取地址经纬度终极版

在读取的时候 加入了 线程池

BaiduApi

/**
 * @author 获取经纬度(地址匹配经纬度需要时间)
 */
public class BaiduApi {
    /**
     * Baidu地图通过地址获取经纬度
     */
    public static String getLngAndLat(String address) {
        String location = "";
        address = address.replace(" ", "");

        String urls="http://api.map.baidu.com/geocoding/v3/?output=json&ak=VsOaLOgYEY55vjviQL17qtWIcmWrkhkn&address96="+address;
        try {
            String json = loadJSON(urls);
            JSONObject obj = JSONObject.parseObject(json);
            if (obj.get("status").toString().equals("0")) {
                // 设置保留小数位数
                java.text.DecimalFormat df =new java.text.DecimalFormat("#.00000");
                // 经度
                double lng = obj.getJSONObject("result").getJSONObject("location").getDouble("lng");
                String newLng=df.format(lng);
                // 纬度
                double lat = obj.getJSONObject("result").getJSONObject("location").getDouble("lat");
                String newLat=df.format(lat);
                System.err.println(newLng);
                location = lng+","+lat;
            } else {
                System.out.println("未找到相匹配的经纬度!");
            }
        } catch (Exception e) {
            System.out.println("未找到相匹配的经纬度,请检查地址!");
        }
        return location;
    }

    public static String loadJSON(String url) {
        StringBuilder json = new StringBuilder();
        try {
            URL oracle = new URL(url);
            URLConnection yc = oracle.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream(), "UTF-8"));
            String inputLine = null;
            while ((inputLine = in.readLine()) != null) {
                json.append(inputLine);
            }
            in.close();
        } catch (MalformedURLException e) {
        } catch (IOException e) {
        }
        return json.toString();
    }
//    public static void main(String[] args) {
//        String location = getLngAndLat("四川省-资阳市-安岳县-东方红小学南(西大街北)152-4号");
//        System.out.println(location);
//    }
}

TestReadFile

public class TestReadFile {

    public static void readColumn(File file, int index) throws Exception {
        InputStream inputStream = new FileInputStream(file.getAbsoluteFile());
        Workbook workbook = Workbook.getWorkbook(inputStream);
        Sheet sheet = workbook.getSheet(0);
        int rows = sheet.getRows();// 行
        int columns = sheet.getColumns();// 列

        ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("readFile-pool-%d").build();
        ExecutorService exec = new ThreadPoolExecutor(5, 20,
                0L, TimeUnit.MILLISECONDS,
                new ArrayBlockingQueue<>(20), namedThreadFactory, new ThreadPoolExecutor.CallerRunsPolicy());
        List<String> locationList = new ArrayList<>();
        // 从第几行开始
        for (int i = 0; i < rows; i++) {
            Cell cell = sheet.getCell(index, i);
            locationList.add(cell.getContents());
            // 读取经纬度
//            BaiduApi.getLngAndLat(cell.getContents());
            // 读取地址
//            System.err.println(cell.getContents());
        }
        locationList.forEach(row -> {
            exec.execute(new Location(row));
        });
    }

    static class Location implements Runnable {
        public String contents;

        public Location(String contents) {
            this.contents = contents;
        }

        @Override
        public void run() {
            BaiduApi.getLngAndLat(contents);
        }
    }

    public static void main(String[] args) {
        File file = new File("C:\\Users\\admin\\Desktop\\翼联科技文件\\项目文件\\6666.xls");
        try {
            System.out.println("正在读取书名...");
            //取出文件地址
            readColumn(file, 9);//读取第一列
            System.out.println("读取完毕");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值