네이버 길찾기 api를 사용하려면,
네이버 클라우드에 등록을 하고 키를 발급 받아야 한다.
맵 관련 서비스는 아래에서 Directions 15 이다.
추가해야 서비스를 사용할 수 있다.

위 이미지(콘솔화면)의 [r]을 클릭하면, 링크가 연결되나,참고로
아래 사이트이다.
https://guide.ncloud-docs.com/docs/ko/naveropenapiv3-maps-direction-15-driving
Directions 15 - Maps
guide.ncloud-docs.com
위 레퍼런스 참조하고 읽어보고 하면 되는데....
바빠죽겠는데.. 언제 읽고 있냐라고 생각하면,,,,
사실 나도 그래서... 찾아봤는데... ㅜㅜ 없어서.. 결국 읽어보고 만들었다.
어려운 코드는 아니지만.. 남이 만든 api 설명서를... 읽기가 귀찮음. 왜냐면... 대부분 성의가 없기 때문임.
그 설명서 작성한 사람도 설명서 작성하기 귀찮아서.. 대충 작성했는데... 그걸 보고 만들려고 하면. 홧병남.
참고로 c#코드 이므로... java는 다를 것이다.
java코드는 나중에 기회되면...하는 걸로....
아래 소스 붙여서 인증 키만 바꿔서 쓰면된다.
c#코드
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | public ActionResult GetDriving(Dictionary< string , object > parameters) { decimal startX = parameters[ "startX" ].xObjectToDecimal(); decimal startY = parameters[ "startY" ].xObjectToDecimal(); decimal endX = parameters[ "endX" ].xObjectToDecimal(); decimal endY = parameters[ "endY" ].xObjectToDecimal(); //https://naveropenapi.apigw.ntruss.com/map-direction-15/v1/driving?start={출발지}&goal={목적지}&option={탐색옵션}" //출발지, 목적지 예시 127.12345,37.12345:128.12345,38.12345 //탐색옵션 //trafast 실시간 빠른길 //tracomfort 실시간 편한길5 //traoptimal 실시간 최적 //traavoidtoll 무료 우선 //traavoidcaronly 자동차 전용도로 회피 우선 NaverDriving myDeserializedClass = null ; HttpClient client = new HttpClient(); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Add( "X-NCP-APIGW-API-KEY-ID" , "6ghfghfgh43xg" ); client.DefaultRequestHeaders.Add( "X-NCP-APIGW-API-KEY" , "k4QRYxdV6aXdfdgfhfghfghmE2jCOjnBak2B6LyXY" ); string url = string .Format( "https://naveropenapi.apigw.ntruss.com/map-direction-15/v1/driving?start={0},{1}&goal={2},{3}&option={4}" , startX, startY, endX, endY, "trafast" ); try { var t = Task.Run(async () => { var response = await client.GetAsync(url); if (response.IsSuccessStatusCode) { string contents = await response.Content.ReadAsStringAsync(); myDeserializedClass = JsonConvert.DeserializeObject<NaverDriving>(contents); }; }); t.Wait(); } catch (AggregateException ex) { throw ex.InnerException; } finally { client.Dispose(); } return Json(myDeserializedClass); } |
model
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | public class NaverDriving { public int code { get ; set ; } public string message { get ; set ; } public DateTime currentDateTime { get ; set ; } public Route route { get ; set ; } } public class Goal { public List< double > location { get ; set ; } public int dir { get ; set ; } } public class Guide { public int pointIndex { get ; set ; } public int type { get ; set ; } public string instructions { get ; set ; } public int distance { get ; set ; } public int duration { get ; set ; } } public class Route { public List<Trafast> trafast { get ; set ; } } public class Section { public int pointIndex { get ; set ; } public int pointCount { get ; set ; } public int distance { get ; set ; } public string name { get ; set ; } public int congestion { get ; set ; } public int speed { get ; set ; } } public class Start { public List< double > location { get ; set ; } } public class Summary { public Start start { get ; set ; } public Goal goal { get ; set ; } public int distance { get ; set ; } public int duration { get ; set ; } public int etaServiceType { get ; set ; } public DateTime departureTime { get ; set ; } public List<List< double >> bbox { get ; set ; } public int tollFare { get ; set ; } public int taxiFare { get ; set ; } public int fuelPrice { get ; set ; } } public class Trafast { public Summary summary { get ; set ; } public List<List< double >> path { get ; set ; } public List<Section> section { get ; set ; } public List<Guide> guide { get ; set ; } } |
api json데이터
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | { "code" : 0, "message" : "길찾기를 성공하였습니다." , "currentDateTime" : "2023-03-06T00:52:43" , "route" : { "trafast" : [{ "summary" : { "start" : { "location" : [126.9810143, 37.5263935] }, "goal" : { "location" : [126.9723026, 37.5128400], "dir" : 2 }, "distance" : 2937, "duration" : 464493, "etaServiceType" : 0, "departureTime" : "2023-03-06T00:52:43" , "bbox" : [ [126.9665270, 37.5128400], [126.9810143, 37.5263935] ], "tollFare" : 0, "taxiFare" : 8160, "fuelPrice" : 332 }, "path" : [ [126.9808575, 37.5250119], [126.9801917, 37.5250595], [126.9794762, 37.5250978], [126.9782060, 37.5251589], [126.9781042, 37.5251440], [126.9780343, 37.5251058], [126.9779838, 37.5250515], [126.9778970, 37.5248384], [126.9776646, 37.5242461], [126.9773766, 37.5235147], [126.9773398, 37.5234289], [126.9773220, 37.5233865], [126.9772498, 37.5231996], [126.9772413, 37.5231211], [126.9772371, 37.5230743], [126.9745249, 37.5163913] ], "section" : [{ "pointIndex" : 27, "pointCount" : 7, "distance" : 482, "name" : "서빙고로" , "congestion" : 2, "speed" : 37 }, { "pointIndex" : 33, "pointCount" : 29, "distance" : 556, "name" : "이촌로51길" , "congestion" : 2, "speed" : 29 }, { "pointIndex" : 61, "pointCount" : 17, "distance" : 540, "name" : "이촌로" , "congestion" : 2, "speed" : 19 }], "guide" : [{ "pointIndex" : 27, "type" : 3, "instructions" : "'용산역, 한강대교' 방면으로 우회전" , "distance" : 676, "duration" : 135485 }, { "pointIndex" : 33, "type" : 2, "instructions" : "이촌역앞에서 '이촌로' 방면으로 좌회전" , "distance" : 482, "duration" : 46896 }, { "pointIndex" : 61, "type" : 2, "instructions" : "'서빙고동, 이촌1동' 방면으로 좌회전" , "distance" : 556, "duration" : 68368 }, { "pointIndex" : 77, "type" : 3, "instructions" : "'이촌한강공원' 방면으로 우회전" , "distance" : 540, "duration" : 99036 }, { "pointIndex" : 92, "type" : 2, "instructions" : "'서빙고로62길' 방면으로 좌회전" , "distance" : 339, "duration" : 49530 }, { "pointIndex" : 101, "type" : 88, "instructions" : "목적지" , "distance" : 344, "duration" : 65178 }] }] } } |
*추가로 위 결과 데이터는 너무 길어서 의미 없어 보이는 일부 데이터는 삭제하였음.
*아래 글 보면, api json 데이터를 c# 모델(class)로 변환해주는 간단한 방법이 있다.
모르면, 한번 읽어보는 것도 좋을 것 같다.
2023.03.06 - [MVC] - Json 데이터를 C# 코드로 변환
Json 데이터를 C# 코드로 변환
Json 데이터를 C#으로 모델을 만들어야 되는데... 귀찮아 하고 있던 중에... 예전에 같이 일했던 상사분이 알려 준 사이트가 생각나다. 그 때는 그다지 필요한 상황이 아니어서, 그냥 네네하고 넘어
captainyellow.tistory.com
'Vue.js' 카테고리의 다른 글
최단거리 구하기(2) - 네비게이션 경로 활용 (4) | 2023.03.23 |
---|---|
최단거리 구하기(1) (25) | 2023.03.22 |
javascript json object 복사 (149) | 2023.02.09 |
vue.js naver map - vworld 폴리곤 데이터 보정 (68) | 2022.11.03 |
DevExtreme DataGrid multi header (vue.js) (2) | 2022.09.17 |
댓글