-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
411 lines (304 loc) · 12.3 KB
/
index.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
<html>
<head>
<meta charset="utf-8">
<!-- Leaflet library-->
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet/v0.7.7/leaflet.css" />
<script src="http://cdn.leafletjs.com/leaflet/v0.7.7/leaflet.js"></script>
<!-- map -->
<link rel="stylesheet" href="custom.css" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<script src="skobbler-2.0.js"></script>
<!-- leaflet -->
<!-- JQuery -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<title>Pedestrian map for Blind</title>
</head>
<body>
<div class="header">
<div id="input_panel" class="panel">
<input id="txtbox_origin" class="panel sub" type="text" placeholder="from" onchange="selectStartPoint(this.value)" onclick="clearSearchResultDiv()"></input>
<input id="txtbox_dest" class="panel sub" type="text" placeholder="to" onchange="selectDestPoint(this.value)" onclick="clearSearchResultDiv()"></input>
<input type="button" class="panel sub bnt" value="GO !!" onclick="calRoute()" />
<div id="search_result_org"></div>
<div id="search_result_dest"></div>
</div>
</header>
<div id="map">
</div>
<div id="searchBox">
<input type="text" id="txt_searchPlace" placeholder="search place" onchange="searchPlace(this.value)"></input>
<div id="search_result"></div>
</div>
<div class="over_map input" id="inputOD">
<div id="div_origin">
<input id="origin" class="search origin" type="text" placeholder="from" onchange="selectStartPoint(this.value)" onclick="clearSearchResultDiv()"></input>
</div>
<div id="div_dest">
<input id="dest" class="search dest" type="text" placeholder="to" onchange="selectDestPoint(this.value)" onclick="clearSearchResultDiv()"></input>
<div id="search_result_dest"></div>
</div>
<div id="div_button_go">
<input type="button" class="bnt inline" value="GO !!" onclick="calRoute()" />
</div>
</div>
</body>
</html>
<script src="map.js"></script>
<script>
var origin,dest,org_lat,org_lon,dest_lat,dest_lon;
var color;
//clear searchBox
document.getElementById('origin').value = "";
document.getElementById('dest').value = "";
//start point marker style
var origin_icon = L.icon({
iconUrl: 'image/start.png',
iconSize: [48, 75], // size of the icon
shadowSize: [50, 64], // size of the shadow
iconAnchor: [22, 74], // point of the icon which will correspond to marker's location
shadowAnchor: [4, 62], // the same for the shadow
popupAnchor: [-3, -76] // point from which the popup should open relative to the iconAnchor
});
//end point marker style
var dest_icon = L.icon({
iconUrl: 'image/dest.png',
iconSize: [48, 75], // size of the icon
shadowSize: [50, 64], // size of the shadow
iconAnchor: [22, 74], // point of the icon which will correspond to marker's location
shadowAnchor: [4, 62], // the same for the shadow
popupAnchor: [-3, -76] // point from which the popup should open relative to the iconAnchor
});
var warning_icon = L.icon({
iconUrl: 'image/warning.png',
iconSize: [38, 45], // size of the icon
shadowSize: [50, 64], // size of the shadow
iconAnchor: [22, 44], // point of the icon which will correspond to marker's location
shadowAnchor: [4, 62], // the same for the shadow
popupAnchor: [-3, -36] // point from which the popup should open relative to the iconAnchor
});
//search
function searchPlace(place){
var search_url = 'http://nominatim.openstreetmap.org/search.php?format=json&q='+place+'&polygon=1&viewbox=';
console.log(search_url);
$.ajax({
url: search_url,
dataType : 'json',
beforeSend : function(){
var div_searchBox = document.getElementById('searchBox');
div_searchBox.innerHTML = div_searchBox.innerHTML + '<img id="loader" src="image/loader.gif">';
},
success: function(res){
document.getElementById('search_result').style.visibility = "visible";
console.log(res);
var div_result = document.getElementById('search_result');
var lat,lon,placeName;
if(res.length == 0)//if no result
div_result.innerHTML = '<div class="search_res noResult">Sorry, no result</div>';
else{
//remove previous result
div_result.innerHTML = '';
//show result
for(var i = 0; i<res.length; i++){
lat = res[i].lat;
lon = res[i].lon;
placeName = res[i].display_name;
div_result.innerHTML = div_result.innerHTML + '<div class="search_res" onclick="moveTo('+lat+","+lon+",'"+placeName+"'"+')">'+placeName+'</div>';
console.log('<div class="search_res" onclick="moveTo('+lat+","+lon+","+placeName.toString()+')">'+placeName+'</div>');
}
}
},
complete: function() {
document.getElementById("loader").remove();
}
});
}
function moveTo(lat,lon,placeName){
var circle;
map.panTo(L.latLng(lat, lon));
document.getElementById('search_result').style.visibility = "hidden";
document.getElementById('txt_searchPlace').value = placeName;
circle_layer.clearLayers();
circle = new L.circle([lat, lon], 300, {
color: 'red',
fillColor: '#f03',
fillOpacity: 0.5
}).addTo(circle_layer);
circle.bindPopup("[lat,lon] : ["+lat+","+lon+"]");
}
function selectStartPoint(str){
//hide search result from Dest
document.getElementById('search_result_dest').style.visibility = "hidden";
document.getElementById('search_result_org').style.visibility = "hidden";
//but not remove previous input in origin input textbox;
document.getElementById('dest').value = dest;
origin = str;
console.log(origin);
//show search result of origin
var res_orgin_req = 'http://nominatim.openstreetmap.org/?format=json&addressdetails=1&q='+origin+'&format=json';
$.ajax({
url: res_orgin_req,
dataType : 'json',
beforeSend : function(){
var div_searchBox = document.getElementById('div_origin');
div_searchBox.innerHTML = div_searchBox.innerHTML + '<img id="loader" src="image/loader.gif">';
},
success: function(res){
console.log(res.length);
document.getElementById('search_result_org').style.visibility = "visible";
var div_result = document.getElementById('search_result_org');
div_result.innerHTML = ''; //clear div
if(res.length == 0){//if no result
div_result.innerHTML = '<div class="search_res">Sorry, no result</div>';
}
else{
for(var i = 0; i<res.length; i++){
lat = res[i].lat;
lon = res[i].lon;
placeName = res[i].display_name;
var shorten_placeName = "'"+placeName.toString()+"'";
div_result.innerHTML = div_result.innerHTML + '<div class="search_res" onclick="setStartPoint('+lat+","+lon+','+shorten_placeName+')">'+placeName+'</div>';
}
}
},
complete: function() {
document.getElementById("loader").remove();
}
});
}
function selectDestPoint(end){
//hide search result from origin
document.getElementById('search_result_org').style.visibility = "hidden";
document.getElementById('origin').value = origin;
dest = end;
console.log(dest);
var res_dest_req = 'http://nominatim.openstreetmap.org/?format=json&addressdetails=1&q='+dest+'&format=json';
$.ajax({
url: res_dest_req,
dataType : 'json',
beforeSend : function(){
var div_searchBox = document.getElementById('div_dest');
div_searchBox.innerHTML = div_searchBox.innerHTML + '<img id="loader" src="image/loader.gif">';
},
success: function(res){
console.log(res);
document.getElementById('search_result_dest').style.visibility = "visible";
var div_result = document.getElementById('search_result_dest');
div_result.innerHTML = ''; //clear div
//show result
if(res.length == 0){//if no result
div_result.innerHTML = '<div class="search_res">Sorry, no result</div>';
}
else{
for(var i = 0; i<res.length; i++){
lat = res[i].lat;
lon = res[i].lon;
placeName = res[i].display_name;
var shorten_placeName = "'"+placeName.toString()+"'";
div_result.innerHTML = div_result.innerHTML + '<div class="search_res" onclick="setDestPoint('+lat+","+lon+','+shorten_placeName+')">'+placeName+'</div>';
}
}
},
complete: function() {
document.getElementById("loader").remove();
}
});
}
function setStartPoint(lat,lon,name){
console.log(lat+" "+lon);
org_lat = lat;
org_lon = lon;
document.getElementById("txtbox_origin").value = name;
origin = document.getElementById('origin').value;
document.getElementById('search_result_org').style.visibility = "hidden";
}
function setDestPoint(lat,lon,name){
console.log(lat+" "+lon);
dest_lat = lat;
dest_lon = lon;
document.getElementById("txtbox_dest").value = name;
dest = document.getElementById('dest').value;
document.getElementById('search_result_dest').style.visibility = "hidden";
}
function clearSearchResultDiv(){
document.getElementById('search_result_org').style.visibility = "hidden";
document.getElementById('search_result_dest').style.visibility = "hidden";
}
function calRoute(){ //calRoute : use Skobbler service
document.getElementById('search_result_org').style.visibility = "hidden";
document.getElementById('search_result_dest').style.visibility = "hidden";
route_layer.clearLayers(); //clear previous route
turningPoint_layer.clearLayers();
badRoute_layer.clearLayers();
var requestRoute = 'http://'+API_KEY+'.tor.skobbler.net/tor/RSngx/calcroute/json/20_5/en/'+API_KEY+'?start='+org_lat+','+org_lon+'&dest='+dest_lat+','+dest_lon+'&profile=pedestrian&advice=yes&points=yes';
//requestRoute = 'http://b53b3acef8808a2c0e5fea3c69912e4b.tor.skobbler.net/tor/RSngx/calcroute/json/20_5/en/b53b3acef8808a2c0e5fea3c69912e4b?start=13.8374743,100.5745865&dest=13.9202243,100.6012091&profile=pedestrian&advice=yes';
console.log(requestRoute.toString());
$.ajax({
url: requestRoute.toString(),
dataType : 'json',
beforeSend : function(){
var div_searchBox = document.getElementById('div_button_go');
div_searchBox.innerHTML = div_searchBox.innerHTML + '<img id="loader" src="image/loader.gif">';
//change cursot to be wait
document.body.style.cursor = 'wait';
},
success: function(res){
document.body.style.cursor = 'default';
console.log(res.route);
var pos_array = res.route;
var lat,lon,dist,inst,marker; //inst == instruction
console.log(res.status.apiMessage);
var routePoint = res.route.routePoints;
//draw route;
var latlng_arr = [],latlng_arr_bad = [], latlng;
for(var i = 0; i < routePoint.length ; i++){
lat = routePoint[i].y;
lon = routePoint[i].x;
latlng = new L.LatLng(lat, lon);
latlng_arr.push(latlng);
//set start point marker
if(i == 0)
marker = L.marker([lat, lon], {icon: origin_icon}).addTo(route_layer);
//set end point marker
if(i == routePoint.length - 1)
marker = L.marker([lat, lon],{icon: dest_icon}).addTo(route_layer);
//random select bad path
var rand_item = latlng_arr[Math.floor(Math.random()*latlng_arr.length)];
//latlng_arr_bad.push(rand_item);
//// DEMO /////
var rand = getRandomInt(0, 3);
if(rand == 3){
var badarea = L.marker([lat, lon], {icon: warning_icon}).addTo(badRoute_layer);
badarea.bindPopup("ซ่อมท่อ");
}
//////////////////////////
}
console.log("bad route"+latlng_arr_bad);
var route_line = new L.Polyline(latlng_arr, {
color: 'blue',
weight: 6,
smoothFactor: 1,
opacity: .7,
dashArray: '20,15',
lineJoin: 'round'
});
route_line.addTo(route_layer);
for(var i = 0; i < pos_array.advisor.length ; i++){
lat = pos_array.advisor[i].coordinates.y;
lon = pos_array.advisor[i].coordinates.x;
dist = pos_array.advisor[i].distance;
inst = pos_array.advisor[i].instruction;
console.log(lat+" , "+lon+" dist: "+dist+" dest: "+inst);
map.panTo(L.latLng(lat, lon));
marker = L.marker([lat, lon]).addTo(turningPoint_layer);
marker.bindPopup(i+") "+inst);
}
},
complete: function() {
document.getElementById("loader").remove();
}
});
}
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
</script>