アドレスによる座標の位置決め
8813 ワード
随筆で遊ぶ
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
";
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;
}
}