JAVA 주소 좌표 구하기 및 거리 측정계산
2020.03.21 11:35
JAVA를 이용하여 주소 좌표를 구한 후 거리 측정하는 모듈
먼저 주소를 좌표로 변경해주는 API를 찾는다.
- 공간정보 오픈플랫폼의 Geocoder Api 2.0을 사용했다.
주소 : http://www.vworld.kr/dev/v4dv_geocoderguide2_s001.do
- 주소를 좌표로 변환하는 서비스를 제공하며 일일 요청건수는 최대 30,000건임.
public static void main(String args[]){
String apiURL = "http://api.vworld.kr/req/address";
try{
int responseCode = 0;
URL url = new URL(apiURL);
HttpURLConnection con = (HttpURLConnection)url.openConnection();
con.setRequestMethod("POST");
String keyword = "서울 영등포구 영중로 134-1 문성빌딩 704호";
String text_content = URLEncoder.encode(keyword.toString(), "utf-8");
//String text_content = URLEncoder.encode(keyword.toString());
// post request
String postParams = "service=address";
postParams += "&request=getcoord";
postParams += "&version=2.0";
postParams += "&crs=EPSG:4326";
postParams += "&address="+text_content;
postParams += "&arefine=true";
postParams += "&simple=false";
postParams += "&format=json";
postParams += "&type=road";
postParams += "&errorFormat=json";
postParams += "&key=발급받은키";
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(postParams);
wr.flush();
wr.close();
responseCode = con.getResponseCode();
BufferedReader br;
if(responseCode==200) { // 정상 호출
br = new BufferedReader(new InputStreamReader(con.getInputStream()));
}else{ // 에러 발생
br = new BufferedReader(new InputStreamReader(con.getErrorStream()));
}
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = br.readLine()) != null) {
response.append(inputLine);
}
System.out.println("response : " + response);
br.close();
con.disconnect();
}catch(Exception e){
e.printStackTrace();
}
}
String apiURL = "http://api.vworld.kr/req/address";
try{
int responseCode = 0;
URL url = new URL(apiURL);
HttpURLConnection con = (HttpURLConnection)url.openConnection();
con.setRequestMethod("POST");
String keyword = "서울 영등포구 영중로 134-1 문성빌딩 704호";
String text_content = URLEncoder.encode(keyword.toString(), "utf-8");
//String text_content = URLEncoder.encode(keyword.toString());
// post request
String postParams = "service=address";
postParams += "&request=getcoord";
postParams += "&version=2.0";
postParams += "&crs=EPSG:4326";
postParams += "&address="+text_content;
postParams += "&arefine=true";
postParams += "&simple=false";
postParams += "&format=json";
postParams += "&type=road";
postParams += "&errorFormat=json";
postParams += "&key=발급받은키";
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(postParams);
wr.flush();
wr.close();
responseCode = con.getResponseCode();
BufferedReader br;
if(responseCode==200) { // 정상 호출
br = new BufferedReader(new InputStreamReader(con.getInputStream()));
}else{ // 에러 발생
br = new BufferedReader(new InputStreamReader(con.getErrorStream()));
}
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = br.readLine()) != null) {
response.append(inputLine);
}
System.out.println("response : " + response);
br.close();
con.disconnect();
}catch(Exception e){
e.printStackTrace();
}
}
위의 좌표를 구한 후 아래의 주소를 통해서 거리 측정
public static void main(String args[]){
double distanceMile = distance(37.533748117, 126.901166019, 37.528296499, 126.906114115, "mile"); //y,x
// 미터(Meter) 단위
double distanceMeter = distance(37.533748117, 126.901166019, 37.528296499, 126.906114115, "meter");
// 킬로미터(Kilo Meter) 단위
double distanceKiloMeter = distance(37.533748117, 126.901166019, 37.528296499, 126.906114115, "kilo");
}
/**
* 두 지점간의 거리 계산
*
* @param lat1 지점 1 위도
* @param lon1 지점 1 경도
* @param lat2 지점 2 위도
* @param lon2 지점 2 경도
* @param unit 거리 표출단위
* @return
*/
private static double distance(double lat1, double lon1, double lat2, double lon2, String unit) {
double theta = lon1 - lon2;
double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.cos(deg2rad(theta));
dist = Math.acos(dist);
dist = rad2deg(dist);
dist = dist * 60 * 1.1515;
if (unit == "kilo") {
dist = dist * 1.609344;
} else if(unit == "meter"){
dist = dist * 1609.344;
}
return (dist);
}
// This function converts decimal degrees to radians
private static double deg2rad(double deg) {
return (deg * Math.PI / 180.0);
}
// This function converts radians to decimal degrees
private static double rad2deg(double rad) {
return (rad * 180 / Math.PI);
}
double distanceMile = distance(37.533748117, 126.901166019, 37.528296499, 126.906114115, "mile"); //y,x
// 미터(Meter) 단위
double distanceMeter = distance(37.533748117, 126.901166019, 37.528296499, 126.906114115, "meter");
// 킬로미터(Kilo Meter) 단위
double distanceKiloMeter = distance(37.533748117, 126.901166019, 37.528296499, 126.906114115, "kilo");
}
/**
* 두 지점간의 거리 계산
*
* @param lat1 지점 1 위도
* @param lon1 지점 1 경도
* @param lat2 지점 2 위도
* @param lon2 지점 2 경도
* @param unit 거리 표출단위
* @return
*/
private static double distance(double lat1, double lon1, double lat2, double lon2, String unit) {
double theta = lon1 - lon2;
double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.cos(deg2rad(theta));
dist = Math.acos(dist);
dist = rad2deg(dist);
dist = dist * 60 * 1.1515;
if (unit == "kilo") {
dist = dist * 1.609344;
} else if(unit == "meter"){
dist = dist * 1609.344;
}
return (dist);
}
// This function converts decimal degrees to radians
private static double deg2rad(double deg) {
return (deg * Math.PI / 180.0);
}
// This function converts radians to decimal degrees
private static double rad2deg(double rad) {
return (rad * 180 / Math.PI);
}
단 거리 측정은 직선거리를 기준으로 측정이 된다.
by hasiki
댓글 0
번호 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|
» | JAVA 주소 좌표 구하기 및 거리 측정계산 | 하서기 | 2020.03.21 | 10477 |
1 | 나만 모르고 있던 – Java 9 (Java9 빠르게 훑어 보기) | 하서기 | 2017.11.06 | 4597 |