调用高德地图API将地名转换成经纬度

今天帮老师完成一个任务,把一个第一列包含了大量地名的数据集,计算出每个地名的相应的经纬度。以便于后面用经纬度进行进一步的运算。

第一步,高德地图密钥申请

进入高德开放平台首页,输入账号登录(如没有账号先进行注册)注册成为开发者。之后点击我的应用-> 创建新应用-> 完成AK密钥申请
在这里插入图片描述

第二步,调用接口把地名转换成经纬度

我们需要调用的API接口是把地理信息转换成经纬度的接口,接口如下:
https://restapi.amap.com/v3/geocode/geo?parameters
在这里插入图片描述
接口返回数据类型是Json,那么直接可以写一个函数,对返回的JSON数据进行解析。代码如下:

    /**
     * 高德地图通过地址获取经纬度
     */
    public static String httpURLConectionGET(String address,String city) {
        //"http://restapi.amap.com/v3/geocode/geo?address=上海市东方明珠&output=JSON&key=xxxxxxxxx";
        String geturl;
        if (city.equals("")) {
            geturl = "http://restapi.amap.com/v3/geocode/geo?key=389880a06e3f893ea46036f030c94700&address="+address;
        }else {
            geturl = "http://restapi.amap.com/v3/geocode/geo?key=389880a06e3f893ea46036f030c94700&address="+address+"&city="+city;
        }

        String location = "";
        try {
            URL url = new URL(geturl);    // 把字符串转换为URL请求地址
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();// 打开连接
            connection.connect();// 连接会话
            // 获取输入流
            BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line;
            StringBuilder sb = new StringBuilder();
            while ((line = br.readLine()) != null) {// 循环读取流
                sb.append(line);
            }
            br.close();// 关闭流
            connection.disconnect();// 断开连接
            JSONObject a = JSON.parseObject(sb.toString());

            JSONArray sddressArr = JSON.parseArray(a.get("geocodes").toString());

            JSONObject c = JSON.parseObject(sddressArr.get(0).toString());
            location = c.get("location").toString();
        }catch (IndexOutOfBoundsException index) {
            throw new IndexOutOfBoundsException();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("失败!");
        }
        return location;
    }

第三步,对Excel文件处理

待处理的Excel文件如下:
在这里插入图片描述
我们的目的是读取第一列的地理信息数据,把地理信息传入API接口,获得返回的经纬度值,把值写入表格的第二列。
首先,使用POI工具对Excel进行处理,讲第一列数据读出来,存到二维数组中。代码如下:

	/**
     * 读写excel
     * */
    public static String[][] readExcel(String path,String city) {
        try {
            File file = new File(path);
            // 创建输入流,读取Excel
            InputStream is = new FileInputStream(file.getAbsolutePath());
            // 获得工作簿对象
            XSSFWorkbook workbook = new XSSFWorkbook(is);
            // 获得所有工作表,0指第一个表格
            XSSFSheet sheet = workbook.getSheetAt(0);
            // 获得行数
            int rows = sheet.getLastRowNum();
            // 获得列数
            int cols = sheet.getPhysicalNumberOfRows();
            String res[][] = new String[rows+1][2];
            res[0][0] = "地点及居住地\n";
            res[0][1] = "经纬度坐标\n";
            // 读取数据
            for (int row = 1; row <= rows; ++row) {
                //获取行,行号作为参数传递给getRow方法,第一行从0开始计算
                XSSFRow r = sheet.getRow(row);
                System.out.println(r.getPhysicalNumberOfCells());
                String location = r.getCell(0).getStringCellValue();
                System.out.println(location);
                String jw = "";
                try {
                    jw = httpURLConectionGET(location , city);
                }catch (IndexOutOfBoundsException e) {
                    try {
                        jw = httpURLConectionGET(location , "");
                    }catch (IndexOutOfBoundsException e1) {
                    }
                }
                res[row][0] = location;
                res[row][1] = jw;
            }
            is.close();
            workbook.close();
            return res;
        }catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

通过接口查询完接口后,把处理好的数据写回到Excel文件中,代码如下:

    public static void except(String path , String str[][]) throws IOException {
        InputStream is = new FileInputStream(path);
        XSSFWorkbook wb = new XSSFWorkbook(is);
        XSSFSheet sheetAt = wb.getSheetAt(0);//第一个工作表
        for (int i = 0;i < str.length; i++) {
            XSSFRow row = sheetAt.getRow(i);//获得一行
            if(row==null){
                row= sheetAt.createRow(i);//row为空 就创建一个
            }
            for (int j = 0; j < str[i].length; j++) {
                boolean lock=false;//是否锁定单元格
                lock=true;//锁定单元格
                XSSFCell cell = row.getCell(j);//获得一个单元格
                if(cell==null){
                    cell = row.createCell(j);//cell为空 就创建一个
                }
                String v=str[i][j];
                cell.setCellType(Cell.CELL_TYPE_STRING);
                cell.setCellValue(v);
            }
        }
        wb.write(new FileOutputStream(new File(path)));
        wb.close();
    }

第四步,总结

至此,我们对该文档的处理完成,处理后的文档如下:
在这里插入图片描述

发布了12 篇原创文章 · 获赞 17 · 访问量 2662

猜你喜欢

转载自blog.csdn.net/qq_36008321/article/details/104485662