안녕하세요 여러 회원님의 도움으로 구글 길찾기(driving direction)를 구현 하는 중에 모르는게 있어 또 회원님들의 도움을 받고자 합니다.
길찾기 구현시 kml은 국내에서 지원이 안된다고 해서 json 형식으로 받아서 구현을 할려고 하는데, json 파싱시 자식노드가 여러개 있을때 어떤식으로 노드에 접근을 해야 되는지 모르겠네요
json 요청 URL은 http://maps.google.com/maps/api/directions/json?origin=39.995877,116.471565&destination=39.99654,116.466713&sensor=false 이
구요, 저게 원하는 값은 routes->legs->steps(이 노드가 반복됩니다)->end_location의 lat,lng 값입니다. JSONObject, JSONArray 클래스와 맴버 변수를 적절히 사용하면 될 것 같은데, 혹시 노드가 여러개 일때 어떻게 값을 받아 오는지 아시는지요? -
json 처리과정은 잘하신 것 같습니다.
그렇지만 그렇게 해도 xml 이던 json이던 공식적인 api 로 접근하는 한 국내 좌표를 넣으면 구글맵 경로가 안 나옵니다.
지난번 답글의 첫번째 링크를 참조하셔서 문서내용에 첨부된 url 주소를 잘 보시기 바랍니다.
거기다가 국내 좌표와 output=dragdir (두번째 방법) 로 처리하면 한줄짜리 json 문자열이 나옵니다.
그걸 가지고 역시 본문에 나오는 polyline 디코딩을 하면 좌표들이 나올 겁니다.
아니면, 이것도 공식적인 방법은 아니지만 이동방향까지 추출하려면 "다음" 방식을 써서 xml로 추출하세요.^^;
매발톱님 감사합니다. 해결 하였습니다.
1. http://maps.google.com/maps?f=d&hl=en..output=dragdir 을 호출
2. json데이터를 받고
3. polyline 디코딩하여 좌표값을 리스트에 담아
4. for문을 돌면서 좌표값에 따라 draw함수가 호출되도록 하였습니다.
위 방식으로 하면 한국에서도 사용가능하다는 말씀이시죠? 여기 북경이라 테스트가 안되네요-__-;
답변 달아주셔셔 정말 감사합니다^^
json으로 받은 경로가 이상하게 나아요... 둘러가기도 하고..직선으로도 나오고 그렇네요
왜 그렇죠? 혹시 하시는 분있으시면...
String Builder는 아래와 같이 사용했구요...
아시는분 답변 부탁드립니다...
StringBuilder urlString = new StringBuilder();
urlString.append("http://maps.google.com/maps?f=d&hl=en");
urlString.append("&saddr=");//from
urlString.append( Double.toString((double)src.getLatitudeE6()/1.0E6 ));
urlString.append(",");
urlString.append( Double.toString((double)src.getLongitudeE6()/1.0E6 ));
urlString.append("&daddr=");//to
urlString.append( Double.toString((double)dest.getLatitudeE6()/1.0E6 ));
urlString.append(",");
urlString.append( Double.toString((double)dest.getLongitudeE6()/1.0E6 ));
urlString.append("&ie=UTF8&0&om=0&output=dragdir");




자답입니다. 노드에서 child 노드로 들어가는 방법이 따로 있는지 몰라서 저는 배열노드에서는 다시 오브젝트로 만들고 접근하는 방법으로 했습니다. 그리고 JSONArray, JSONObject 객체들이 마구잡이로 선언되어 있어서 이부분도 초기화 해주는 부분을 넣어야 할 것 같은데요 잘모르겠네요~~
StringBuffer json_data = new StringBuffer();List<HashMap<String, String>> list_loc_point = new ArrayList<HashMap<String,String>>();
HashMap<String, String> loc_point = new HashMap<String, String>();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String url = "http://maps.google.com/maps/api/directions/json?origin=39.995877,116.471565&destination=39.99654,116.466713&sensor=false";
getJsonData(url);
try {
parseForJson();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.d("Test", "error : ", e);
}
}
public void parseForJson() throws JSONException{
JSONObject json = new JSONObject(json_data.toString());
JSONArray jarray = json.getJSONArray("routes");
JSONObject routes = jarray.getJSONObject(0); //routes를 오프젝트로 만듬
JSONArray arr_legs = routes.getJSONArray("legs"); // routes -> legs 노드로 이동
JSONObject legs = arr_legs.getJSONObject(0);
JSONArray arr_steps = legs.getJSONArray("steps"); // routes -> legs -> steps 노드로 이동
for(int i = 0; i < arr_steps.length(); i++) {
// 결과별로 결과 object 얻기
JSONObject jtmp = arr_steps.getJSONObject(i);
JSONObject end_location = jtmp.getJSONObject("end_location");
loc_point.put("lat", end_location.getString("lat"));
loc_point.put("lng", end_location.getString("lng"));
list_loc_point.add(loc_point);
// 전체 주소 얻기
}
}
public void getJsonData(String urlStr) {
String line = null;
BufferedReader buffer = null;
try {
URL url = new URL("urlStr);
HttpURLConnection urlConn = (HttpURLConnection)url.openConnection();
//IO 스트림을 이용해 데이타를 읽는다.
buffer = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
while((line = buffer.readLine()) != null){
json_data.append(line);
}
} catch (Exception e) {
// TODO: handle exception
Log.d("Test", "catch : ", e);
} finally {
try {
buffer.close();
} catch (Exception e2) {
// TODO: handle exception
Log.d("Test", "catch : ", e2);
}
}
}