欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

通过地址定位坐标

发布时间:2025/3/15 编程问答 33 豆豆
生活随笔 收集整理的这篇文章主要介绍了 通过地址定位坐标 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

随笔写来玩玩

public class getXYByAliyunAPI {public static void main(String[] args) {Connection conn =null;FileOutputStream fis =null;try {conn = JDBCUtil.getConnection();PreparedStatement pstm = conn.prepareStatement("select unitCode,REPLACE(unitAddress, '#', '') from useWaterUnit");ResultSet rs =pstm.executeQuery();while(rs.next()){String unitAddress = rs.getString(2);String unitCode = rs.getString(1)+",";String string= sendGet("http://gc.ditu.aliyun.com/geocoding", "a="+unitAddress+"&c=成都");string = string.replace("}", "");string = string.replace("{", "");string = string.replace("\"", "");string = string.replace(":", "");String[] strings = string.split(",");for (String s : strings) {s = s.trim();if(s.indexOf("lon")!=-1){unitCode+=s.substring(s.indexOf("lon") + 3, s.length())+",";System.out.println(unitCode+s.substring(s.indexOf("lon") + 3, s.length()));}if(s.indexOf("lat")!=-1){unitCode+=s.substring(s.indexOf("lat") + 3, s.length())+",\r\n";System.out.println(unitCode+s.substring(s.indexOf("lat") + 3, s.length()));}}fis = new FileOutputStream("d:/001.txt", true);fis.write(unitCode.getBytes());fis.flush();}} catch (SQLException e) {e.printStackTrace();} catch (ClassNotFoundException e) {e.printStackTrace();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {try {if(null!=conn){conn.close();}if(null!=fis){fis.close();}} catch (SQLException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}}public static String sendGet(String url, String param) {String result = "";BufferedReader in = null;try {String urlNameString = url + "?" + param;URL realUrl = new URL(urlNameString);// 打开和URL之间的连接URLConnection connection = realUrl.openConnection();// 设置通用的请求属性connection.setRequestProperty("user-agent", "directclient");// 建立实际的连接 connection.connect();// 获取所有响应头字段Map<String, List<String>> map = connection.getHeaderFields();// 遍历所有的响应头字段for (String key : map.keySet()) {// System.out.println(key + "--->" + map.get(key)); }// 定义 BufferedReader输入流来读取URL的响应in = new BufferedReader(new InputStreamReader(connection.getInputStream()));String line;while ((line = in.readLine()) != null) {result += line;}} catch (Exception e) {System.out.println("发送GET请求出现异常!" + e);e.printStackTrace();}// 使用finally块来关闭输入流finally {try {if (in != null) {in.close();}} catch (Exception e2) {e2.printStackTrace();}}return result;} }

 

  

转载于:https://www.cnblogs.com/learningchencheng/p/4630718.html

总结

以上是生活随笔为你收集整理的通过地址定位坐标的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。