-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDirectionsTravelMode.html
155 lines (153 loc) · 6.72 KB
/
DirectionsTravelMode.html
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8" />
<title>設置サンプル - GMAPv3 - Directions - 2地点間のルート検索</title>
<link rel="stylesheet" type="text/css" href="/content/css/gmapv3.css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
var renderFLG=false;
var directionsDisplay;
var directionsService=new google.maps.DirectionsService();
var map,mode;
var currentDirections=null;
var startSpot="東京駅";
var endSpot="六本木ヒルズ";
initialize();
/* 地図初期化 */
function initialize() {
var myOptions={
zoom:14,
center: new google.maps.LatLng(35.670236,139.749832),//虎の門
mapTypeId: google.maps.MapTypeId.ROADMAP
}
/* 地図オブジェクト生成 */
map=new google.maps.Map(document.getElementById("map"), myOptions);
if(!renderFLG) render();
calcRoute(startSpot,endSpot);
}
/* ルート検索結果を描画 */
function render(){
dbg("render:"+renderFLG);
renderFLG=true;
/* ルートをレンダリング */
directionsDisplay=new google.maps.DirectionsRenderer({
"map": map,
"preserveViewport": true,
"draggable": true
});
/* 右カラムにルート表示 */
directionsDisplay.setPanel(document.getElementById("directions_panel"));
/* 出発地点・到着地点マーカーが移動された時 */
google.maps.event.addListener(directionsDisplay, 'directions_changed',function() {
currentDirections=directionsDisplay.getDirections();
var route=currentDirections.routes[0];
var s="";
for(var i=0; i<route.legs.length; i++) {
var routeSegment=i+1;
s+=route.legs[i].start_address+'to';
s+=route.legs[i].end_address+'\n';
s+=route.legs[i].distance.text;
}
dbg("directions_changed:"+s);
});
}
/* モード変更 */
$("#mode").bind("change",function(){
$(".button-group button").removeClass("active");
calcRoute(startSpot,endSpot);
$("#show").addClass("active");
});
/* ルート算出 */
function calcRoute(startSpot,endSpot){
switch($("#mode").val()){
case "driving":
mode=google.maps.DirectionsTravelMode.DRIVING;
break;
case "bicycling":
mode=google.maps.DirectionsTravelMode.BICYCLING;
break;
case "transit":
mode=google.maps.DirectionsTravelMode.TRANSIT;
break;
case "walking":
mode=google.maps.DirectionsTravelMode.WALKING;
break;
}
if(!renderFLG) render();
var request={
origin:startSpot, /* 出発地点 */
destination:endSpot, /* 到着地点 */
travelMode:mode /* 交通手段 */
};
/* ルート描画 */
directionsService.route(request, function(response, status) {
if (status==google.maps.DirectionsStatus.OK) {
dbg(response);
directionsDisplay.setDirections(response);
}else{
dbg("status:"+status);
}
});
}
/* ルート表示・非表示切り替え */
$(".button-group button").click(function(e){
$(".button-group button").removeClass("active");
var id=$(this).attr("id");
if(id=="show"){
calcRoute(startSpot,endSpot);
$(this).addClass("active");
}else{
$(this).addClass("active");
reset();
}
});
/* ルート削除 */
function reset(){
currentDirections=null;
directionsDisplay.setMap(null);
renderFLG=false;
}
});
var dbg=function(str){
try{
if(window.console && console.log){
console.log(str);
}
}catch(err){
//alert("error:"+err);
}
}
</script>
<style>
#map { float:left; width:70%; height:100%; }
#side { float:right; width:30%; height:100%; }
#side .inner { padding:10px; overflow:auto; }
</style>
</head>
<body>
<h1>設置サンプル - GMAPv3 - Directions - 2地点間のルート検索</h2>
<p>「東京駅」から「六本木ヒルズ」までルートを検索し、右カラムにルート検索結果を表示します。 出発地点A・到着地点Bのマーカーをドラッグすると、それぞれの地点を変更されます。右カラムにあるボタンでルートの表示・非表示、プルダウンでトラベルモード(自動車、自転車、電車、徒歩)を切り替えられます。</p>
<div id="map"><!-- 地図の埋め込み表示 --></div>
<div id="side">
<div class="inner">
<p>
<label for="mode">モード:<select id="mode" name="mode">
<option value="driving" selected>DRIVING(自動車)</option>
<option value="bicycling">BICYCLING (自転車)</option>
<option value="transit">TRANSIT(電車)</option>
<option value="walking">WALKING(徒歩)</option>
</select></label>
</p>
<div class="button-group">
<button id="show" class="button active">ルート表示</button>
<button id="hide" class="button">ルート非表示</button>
</div>
<div id="directions_panel" style="width:100%"></div>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAbjUB2rs71Upy9xiAUV--rlqhaOeiUIW0&callback=initMap"></script>
</div>
</div><!-- #side -->
<br clear="all" />
</body>
</html>