교통정보 경로선 표시
지도에 교통정보 경로선 그리기 예제입니다.
// 지도를 생성합니다.
var map = new routo.maps.Map("map", {
center: { lat: 37.507009, lng: 127.0586339 }, // 지도 초기 위치
zoom: 14, // 지도 로딩 시 최초 표시 레벨
});
// 경로 탐색 요청 정보입니다.
var parameters = {
RPOption: [{
"FeeOption": 0, //요금옵션 (0: 모든도로 , 1: 무료도로)
"RoadOption": 0, //우선도로 옵션 (0: 모든도로 , 1: 고속도로우선 ,2:고속도로회피 , 3: 자동차전용도로회피)
"RouteOption": 0 // 탐색옵션 ( 0: 최소시간, 1: 미정의, 2:최단 , 3:추천 )
}],
CoordType: 2, // 좌표계 타입( 1:Bessel , 2: WGS84)
CarType: 0, // 단말 차종 (0: 미선택 ,1: 승용차/소형합차 ,2:중혐승합차, 3:대형승합차 ,4:대형화물차 ,5:특수화물차 ,6:경차)
StartPoint: {
"XPos": 127.0667805, // 출발지 X좌표
"YPos": 37.5052583, // 출발지 Y좌표
"Name": "" // 출발지 명칭
},
GoalPoint: {
"XPos": 127.0514888, // 도착지 X좌표
"YPos": 37.5145472, // 도착지 Y좌표
"Name": "" // 도착지 명칭
}
};
// 경로를 요청합니다.
fetch('https://api.routo.com/v1/directions/basic?key=12290cd4-7094-43e8-a936-8819196d0332', {
mode: "cors",
method: "post",
cache: "no-cache",
async: false,
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(parameters)
}).then(function (response) {
// 경로 요청 결과 JSON을 반환합니다.
return response.json();
}).then(function (json) {
// 경로 요청 결과 JSON을 읽습니다.
var jsonTraffic = routo.maps.Traffic.trafficRead(json);
routo.maps.Traffic.drawRouteLineByTraffic(map, jsonTraffic);
}).catch(function (error) {
// 오류 발생 시 console 및 알림으로 표시합니다.
console.log(error);
alert("상세정보 검색에 실패했습니다.");
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>지도 교통정보 경로선 그리기</title>
<style>
html,
body {
margin: 0;
padding: 0px 15px 10px 15px;
width: 100%;
height: 100%;
}
.map {
width: 100%;
height: 400px;
position: relative;
}
</style>
</head>
<body>
<h2>지도 교통정보 경로선 그리기</h2>
<hr />
<p>
지도에 교통정보 경로선 그리기 예제입니다.
</p>
<div id="map" class="map"></div>
<script src="https://api.routo.com/v2/maps/map?key={APIKEY}"></script>
<script>
// 지도를 생성합니다.
var map = new routo.maps.Map("map", {
center: { lat: 37.507009, lng: 127.0586339 }, // 지도 초기 위치
zoom: 14, // 지도 로딩 시 최초 표시 레벨
});
// 경로 탐색 요청 정보입니다.
var parameters = {
RPOption: [{
"FeeOption": 0, //요금옵션 (0: 모든도로 , 1: 무료도로)
"RoadOption": 0, //우선도로 옵션 (0: 모든도로 , 1: 고속도로우선 ,2:고속도로회피 , 3: 자동차전용도로회피)
"RouteOption": 0 // 탐색옵션 ( 0: 최소시간, 1: 미정의, 2:최단 , 3:추천 )
}],
CoordType: 2, // 좌표계 타입( 1:Bessel , 2: WGS84)
CarType: 0, // 단말 차종 (0: 미선택 ,1: 승용차/소형합차 ,2:중혐승합차, 3:대형승합차 ,4:대형화물차 ,5:특수화물차 ,6:경차)
StartPoint: {
"XPos": 127.0667805, // 출발지 X좌표
"YPos": 37.5052583, // 출발지 Y좌표
"Name": "" // 출발지 명칭
},
GoalPoint: {
"XPos": 127.0514888, // 도착지 X좌표
"YPos": 37.5145472, // 도착지 Y좌표
"Name": "" // 도착지 명칭
}
};
// 경로를 요청합니다.
fetch('https://api.routo.com/v1/directions/basic?key=12290cd4-7094-43e8-a936-8819196d0332', {
mode: "cors",
method: "post",
cache: "no-cache",
async: false,
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(parameters)
}).then(function (response) {
// 경로 요청 결과 JSON을 반환합니다.
return response.json();
}).then(function (json) {
// 경로 요청 결과 JSON을 읽습니다.
var jsonTraffic = routo.maps.Traffic.trafficRead(json);
routo.maps.Traffic.drawRouteLineByTraffic(map, jsonTraffic);
}).catch(function (error) {
// 오류 발생 시 console 및 알림으로 표시합니다.
console.log(error);
alert("상세정보 검색에 실패했습니다.");
});
</script>
</body>
</html>