Check out the latest version of Routino: svn co http://routino.org/svn/trunk routino
Annotation of /trunk/web/www/routino/router.js
Parent Directory
|
Revision Log
Revision 1011 -
(hide annotations)
(download)
(as text)
Sat Jun 30 17:45:35 2012 UTC (12 years, 8 months ago) by amb
File MIME type: application/javascript
File size: 42167 byte(s)
Sat Jun 30 17:45:35 2012 UTC (12 years, 8 months ago) by amb
File MIME type: application/javascript
File size: 42167 byte(s)
Remove some temporary variables by combining statements.
1 | amb | 569 | // |
2 | // Routino router web page Javascript | ||
3 | // | ||
4 | // Part of the Routino routing software. | ||
5 | // | ||
6 | amb | 984 | // This file Copyright 2008-2012 Andrew M. Bishop |
7 | amb | 569 | // |
8 | // This program is free software: you can redistribute it and/or modify | ||
9 | // it under the terms of the GNU Affero General Public License as published by | ||
10 | // the Free Software Foundation, either version 3 of the License, or | ||
11 | // (at your option) any later version. | ||
12 | // | ||
13 | // This program is distributed in the hope that it will be useful, | ||
14 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
16 | // GNU Affero General Public License for more details. | ||
17 | // | ||
18 | // You should have received a copy of the GNU Affero General Public License | ||
19 | // along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
20 | // | ||
21 | |||
22 | amb | 987 | // The number of waypoints to include in the HTML |
23 | var maxmarkers=9; | ||
24 | amb | 574 | |
25 | amb | 987 | var vismarkers, markers, markersmoved, paramschanged; |
26 | amb | 985 | var homelat=null, homelon=null; |
27 | |||
28 | |||
29 | //////////////////////////////////////////////////////////////////////////////// | ||
30 | /////////////////////////////// Initialisation ///////////////////////////////// | ||
31 | //////////////////////////////////////////////////////////////////////////////// | ||
32 | |||
33 | amb | 577 | // Make a deep copy of the routino profile. |
34 | amb | 569 | |
35 | amb | 577 | var routino_default={}; |
36 | for(var l1 in routino) | ||
37 | if(typeof(routino[l1])!='object') | ||
38 | routino_default[l1]=routino[l1]; | ||
39 | else | ||
40 | { | ||
41 | routino_default[l1]={}; | ||
42 | for(var l2 in routino[l1]) | ||
43 | if(typeof(routino[l1][l2])!='object') | ||
44 | routino_default[l1][l2]=Number(routino[l1][l2]); | ||
45 | else | ||
46 | { | ||
47 | routino_default[l1][l2]={}; | ||
48 | for(var l3 in routino[l1][l2]) | ||
49 | routino_default[l1][l2][l3]=Number(routino[l1][l2][l3]); | ||
50 | } | ||
51 | } | ||
52 | |||
53 | amb | 985 | // Store the latitude and longitude in the routino variable |
54 | amb | 577 | |
55 | amb | 985 | routino.point=[]; |
56 | for(var marker=1;marker<=maxmarkers;marker++) | ||
57 | { | ||
58 | routino.point[marker]={}; | ||
59 | |||
60 | routino.point[marker].lon=""; | ||
61 | routino.point[marker].lat=""; | ||
62 | amb | 1001 | routino.point[marker].search=""; |
63 | amb | 985 | routino.point[marker].active=false; |
64 | } | ||
65 | |||
66 | // Process the URL query string and extract the arguments | ||
67 | |||
68 | var legal={"^lon" : "^[-0-9.]+$", | ||
69 | "^lat" : "^[-0-9.]+$", | ||
70 | "^zoom" : "^[0-9]+$", | ||
71 | |||
72 | "^lon[1-9]" : "^[-0-9.]+$", | ||
73 | "^lat[1-9]" : "^[-0-9.]+$", | ||
74 | amb | 1001 | "^search[1-9]" : "^.+$", |
75 | amb | 985 | "^transport" : "^[a-z]+$", |
76 | "^highway-[a-z]+" : "^[0-9.]+$", | ||
77 | "^speed-[a-z]+" : "^[0-9.]+$", | ||
78 | "^property-[a-z]+" : "^[0-9.]+$", | ||
79 | "^oneway" : "^(1|0|true|false|on|off)$", | ||
80 | "^turns" : "^(1|0|true|false|on|off)$", | ||
81 | "^weight" : "^[0-9.]+$", | ||
82 | "^height" : "^[0-9.]+$", | ||
83 | "^width" : "^[0-9.]+$", | ||
84 | "^length" : "^[0-9.]+$", | ||
85 | |||
86 | "^language" : "^[-a-zA-Z]+$"}; | ||
87 | |||
88 | var args={}; | ||
89 | |||
90 | if(location.search.length>1) | ||
91 | { | ||
92 | var query,queries; | ||
93 | |||
94 | query=location.search.replace(/^\?/,""); | ||
95 | query=query.replace(/;/g,'&'); | ||
96 | queries=query.split('&'); | ||
97 | |||
98 | for(var i=0;i<queries.length;i++) | ||
99 | { | ||
100 | queries[i].match(/^([^=]+)(=(.*))?$/); | ||
101 | |||
102 | k=RegExp.$1; | ||
103 | v=unescape(RegExp.$3); | ||
104 | |||
105 | for(var l in legal) | ||
106 | { | ||
107 | if(k.match(RegExp(l)) && v.match(RegExp(legal[l]))) | ||
108 | args[k]=v; | ||
109 | } | ||
110 | } | ||
111 | } | ||
112 | |||
113 | |||
114 | amb | 987 | // |
115 | // Fill in the HTML - add the missing waypoints | ||
116 | // | ||
117 | |||
118 | function html_init() | ||
119 | { | ||
120 | var waypoints=document.getElementById("waypoints"); | ||
121 | amb | 1005 | |
122 | amb | 987 | var waypoint_html=waypoints.rows[0].innerHTML; |
123 | amb | 1005 | waypoints.deleteRow(0); |
124 | amb | 987 | |
125 | amb | 1005 | var searchresults_html=waypoints.rows[0].innerHTML; |
126 | amb | 987 | waypoints.deleteRow(0); |
127 | |||
128 | for(var marker=maxmarkers;marker>=1;marker--) | ||
129 | { | ||
130 | amb | 1005 | var searchresults=waypoints.insertRow(0); |
131 | amb | 1011 | |
132 | amb | 1005 | searchresults.id="searchresults" + marker; |
133 | amb | 1011 | searchresults.innerHTML=searchresults_html.split('XXX').join(marker); |
134 | amb | 987 | |
135 | amb | 1011 | var waypoint=waypoints.insertRow(0); |
136 | amb | 1005 | |
137 | waypoint.id="waypoint" + marker; | ||
138 | amb | 1011 | waypoint.innerHTML=waypoint_html.split('XXX').join(marker); |
139 | amb | 987 | } |
140 | |||
141 | vismarkers=maxmarkers; | ||
142 | } | ||
143 | |||
144 | |||
145 | amb | 577 | //////////////////////////////////////////////////////////////////////////////// |
146 | //////////////////////////////// Form handling ///////////////////////////////// | ||
147 | //////////////////////////////////////////////////////////////////////////////// | ||
148 | |||
149 | amb | 569 | // |
150 | // Form initialisation - fill in the uninitialised parts | ||
151 | // | ||
152 | |||
153 | function form_init() | ||
154 | { | ||
155 | amb | 985 | // Fill in the waypoints |
156 | amb | 569 | |
157 | amb | 985 | var filled=0; |
158 | amb | 569 | |
159 | amb | 985 | for(var marker=maxmarkers;marker>=1;marker--) |
160 | amb | 569 | { |
161 | amb | 985 | var lon=args["lon" + marker]; |
162 | var lat=args["lat" + marker]; | ||
163 | amb | 1001 | var search=args["search" + marker]; |
164 | amb | 577 | |
165 | amb | 1001 | if(lon != undefined && lat != undefined && search != undefined && lon != "" && lat != "" && search != "") |
166 | amb | 569 | { |
167 | amb | 1001 | formSetSearch(marker,search); |
168 | amb | 985 | formSetCoords(marker,lon,lat,true); |
169 | amb | 569 | |
170 | amb | 1001 | markerSearch(marker); |
171 | |||
172 | amb | 985 | filled++; |
173 | amb | 569 | } |
174 | amb | 1001 | else if(lon != undefined && lat != undefined && lon != "" && lat != "") |
175 | { | ||
176 | formSetCoords(marker,lon,lat,true); | ||
177 | |||
178 | markerCoords(marker); | ||
179 | |||
180 | filled++; | ||
181 | } | ||
182 | else if(search != undefined && search != "") | ||
183 | { | ||
184 | formSetSearch(marker,search); | ||
185 | |||
186 | markerSearch(marker); | ||
187 | |||
188 | DoSearch(marker); | ||
189 | |||
190 | filled++; | ||
191 | } | ||
192 | amb | 985 | else if(filled==0) |
193 | markerRemove(marker); | ||
194 | } | ||
195 | amb | 569 | |
196 | amb | 985 | // Update the transport type with the URL settings which updates all HTML forms to defaults. |
197 | amb | 574 | |
198 | amb | 985 | var transport=routino.transport; |
199 | amb | 569 | |
200 | amb | 985 | if(args["transport"] != undefined) |
201 | transport=args["transport"]; | ||
202 | amb | 577 | |
203 | amb | 985 | formSetTransport(transport); |
204 | amb | 577 | |
205 | amb | 985 | // Update the HTML with the URL settings |
206 | |||
207 | if(args["language"] != undefined) | ||
208 | formSetLanguage(args["language"]); | ||
209 | |||
210 | for(var key in routino.profile_highway) | ||
211 | if(args["highway-" + key] != undefined) | ||
212 | formSetHighway(key,args["highway-" + key]); | ||
213 | |||
214 | for(var key in routino.profile_speed) | ||
215 | if(args["speed-" + key] != undefined) | ||
216 | formSetSpeed(key,args["speed-" + key]); | ||
217 | |||
218 | for(var key in routino.profile_property) | ||
219 | if(args["property-" + key] != undefined) | ||
220 | formSetProperty(key,args["property-" + key]); | ||
221 | |||
222 | for(var key in routino.restrictions) | ||
223 | amb | 574 | { |
224 | amb | 985 | if(key=="oneway" || key=="turns") |
225 | amb | 577 | { |
226 | amb | 985 | if(args[key] != undefined) |
227 | formSetRestriction(key,args[key]); | ||
228 | amb | 577 | } |
229 | amb | 985 | else |
230 | { | ||
231 | if(args["restrict-" + key] != undefined) | ||
232 | formSetRestriction(key,args["restrict-" + key]); | ||
233 | } | ||
234 | amb | 574 | } |
235 | amb | 569 | |
236 | amb | 577 | // Get the home location cookie and compare to each waypoint |
237 | |||
238 | var cookies=document.cookie.split('; '); | ||
239 | |||
240 | for(var cookie=0;cookie<cookies.length;cookie++) | ||
241 | if(cookies[cookie].substr(0,"Routino-home".length)=="Routino-home") | ||
242 | { | ||
243 | var data=cookies[cookie].split(/[=:;]/); | ||
244 | |||
245 | if(data[1]=="lon") homelon=Number(data[2]); | ||
246 | if(data[3]=="lat") homelat=Number(data[4]); | ||
247 | } | ||
248 | |||
249 | if(homelon!=null && homelat!=null) | ||
250 | { | ||
251 | amb | 985 | for(var marker=maxmarkers;marker>=1;marker--) |
252 | amb | 577 | { |
253 | amb | 985 | var lon=routino.point[marker].lon; |
254 | var lat=routino.point[marker].lat; | ||
255 | amb | 577 | |
256 | if(lon==homelon && lat==homelat) | ||
257 | updateIcon(marker); | ||
258 | } | ||
259 | |||
260 | // If the first location is empty and the cookie is set then fill it. | ||
261 | |||
262 | amb | 985 | if(routino.point[1].lon=="" && routino.point[1].lat=="") |
263 | formSetCoords(1,homelon,homelat,true); | ||
264 | amb | 577 | } |
265 | amb | 569 | } |
266 | |||
267 | |||
268 | // | ||
269 | amb | 577 | // Change of language in the form |
270 | // | ||
271 | |||
272 | amb | 985 | function formSetLanguage(value) |
273 | amb | 577 | { |
274 | amb | 985 | if(value == undefined) |
275 | { | ||
276 | for(var lang=0;lang<document.forms["form"].elements["language"].length;lang++) | ||
277 | if(document.forms["form"].elements["language"][lang].checked) | ||
278 | routino.language=document.forms["form"].elements["language"][lang].value; | ||
279 | } | ||
280 | else | ||
281 | { | ||
282 | for(var lang=0;lang<document.forms["form"].elements["language"].length;lang++) | ||
283 | if(document.forms["form"].elements["language"][lang].value==value) | ||
284 | document.forms["form"].elements["language"][lang].checked=true; | ||
285 | else | ||
286 | document.forms["form"].elements["language"][lang].checked=false; | ||
287 | amb | 577 | |
288 | amb | 985 | routino.language=value; |
289 | } | ||
290 | amb | 577 | } |
291 | |||
292 | |||
293 | // | ||
294 | amb | 569 | // Change of transport in the form |
295 | // | ||
296 | |||
297 | amb | 985 | function formSetTransport(value) |
298 | amb | 569 | { |
299 | amb | 985 | routino.transport=value; |
300 | amb | 569 | |
301 | amb | 577 | for(var key in routino.transports) |
302 | document.forms["form"].elements["transport"][routino.transports[key]-1].checked=(key==routino.transport); | ||
303 | amb | 569 | |
304 | amb | 577 | for(var key in routino.profile_highway) |
305 | document.forms["form"].elements["highway-" + key].value=routino.profile_highway[key][routino.transport]; | ||
306 | amb | 569 | |
307 | amb | 577 | for(var key in routino.profile_speed) |
308 | document.forms["form"].elements["speed-" + key].value=routino.profile_speed[key][routino.transport]; | ||
309 | amb | 569 | |
310 | amb | 577 | for(var key in routino.profile_property) |
311 | document.forms["form"].elements["property-" + key].value=routino.profile_property[key][routino.transport]; | ||
312 | amb | 569 | |
313 | amb | 577 | for(var key in routino.restrictions) |
314 | amb | 569 | { |
315 | amb | 622 | if(key=="oneway" || key=="turns") |
316 | amb | 577 | document.forms["form"].elements["restrict-" + key].checked=routino.profile_restrictions[key][routino.transport]; |
317 | amb | 569 | else |
318 | amb | 577 | document.forms["form"].elements["restrict-" + key].value=routino.profile_restrictions[key][routino.transport]; |
319 | amb | 569 | } |
320 | |||
321 | paramschanged=true; | ||
322 | } | ||
323 | |||
324 | |||
325 | // | ||
326 | // Change of highway in the form | ||
327 | // | ||
328 | |||
329 | amb | 985 | function formSetHighway(type,value) |
330 | amb | 569 | { |
331 | amb | 985 | if(value == undefined) |
332 | routino.profile_highway[type][routino.transport]=document.forms["form"].elements["highway-" + type].value; | ||
333 | else | ||
334 | { | ||
335 | document.forms["form"].elements["highway-" + type].value=value; | ||
336 | routino.profile_highway[type][routino.transport]=value; | ||
337 | } | ||
338 | amb | 569 | |
339 | paramschanged=true; | ||
340 | } | ||
341 | |||
342 | |||
343 | // | ||
344 | // Change of Speed in the form | ||
345 | // | ||
346 | |||
347 | amb | 985 | function formSetSpeed(type,value) |
348 | amb | 569 | { |
349 | amb | 985 | if(value == undefined) |
350 | routino.profile_speed[type][routino.transport]=document.forms["form"].elements["speed-" + type].value; | ||
351 | else | ||
352 | { | ||
353 | document.forms["form"].elements["speed-" + type].value=value; | ||
354 | routino.profile_speed[type][routino.transport]=value; | ||
355 | } | ||
356 | amb | 569 | |
357 | paramschanged=true; | ||
358 | } | ||
359 | |||
360 | |||
361 | // | ||
362 | amb | 574 | // Change of Property in the form |
363 | // | ||
364 | |||
365 | amb | 985 | function formSetProperty(type,value) |
366 | amb | 574 | { |
367 | amb | 985 | if(value == undefined) |
368 | routino.profile_property[type][routino.transport]=document.forms["form"].elements["property-" + type].value; | ||
369 | else | ||
370 | { | ||
371 | document.forms["form"].elements["property-" + type].value=value; | ||
372 | routino.profile_property[type][routino.transport]=value; | ||
373 | } | ||
374 | amb | 574 | |
375 | paramschanged=true; | ||
376 | } | ||
377 | |||
378 | |||
379 | // | ||
380 | amb | 622 | // Change of Restriction rule in the form |
381 | amb | 569 | // |
382 | |||
383 | amb | 985 | function formSetRestriction(type,value) |
384 | amb | 569 | { |
385 | amb | 985 | if(value == undefined) |
386 | { | ||
387 | if(type=="oneway" || type=="turns") | ||
388 | routino.profile_restrictions[type][routino.transport]=document.forms["form"].elements["restrict-" + type].checked; | ||
389 | else | ||
390 | routino.profile_restrictions[type][routino.transport]=document.forms["form"].elements["restrict-" + type].value; | ||
391 | } | ||
392 | amb | 569 | else |
393 | amb | 985 | { |
394 | if(type=="oneway" || type=="turns") | ||
395 | document.forms["form"].elements["restrict-" + type].checked=value; | ||
396 | else | ||
397 | document.forms["form"].elements["restrict-" + type].value=value; | ||
398 | amb | 569 | |
399 | amb | 985 | routino.profile_restrictions[type][routino.transport]=value; |
400 | } | ||
401 | |||
402 | amb | 569 | paramschanged=true; |
403 | } | ||
404 | |||
405 | |||
406 | // | ||
407 | amb | 577 | // Set the feature coordinates from the form when the form changes. |
408 | // | ||
409 | |||
410 | amb | 985 | function formSetCoords(marker,lon,lat,active) |
411 | amb | 577 | { |
412 | amb | 1005 | clearSearchResult(marker); |
413 | |||
414 | amb | 985 | if(lon == undefined || lat == undefined) |
415 | { | ||
416 | routino.point[marker].lon=document.forms["form"].elements["lon" + marker].value; | ||
417 | routino.point[marker].lat=document.forms["form"].elements["lat" + marker].value; | ||
418 | |||
419 | if(routino.point[marker].lon=="" || routino.point[marker].lat=="") | ||
420 | markerCentre(marker); | ||
421 | } | ||
422 | else | ||
423 | { | ||
424 | amb | 1001 | document.forms["form"].elements["lon" + marker].value=format5f(lon); |
425 | document.forms["form"].elements["lat" + marker].value=format5f(lat); | ||
426 | amb | 985 | |
427 | routino.point[marker].lon=lon; | ||
428 | routino.point[marker].lat=lat; | ||
429 | |||
430 | if(active != undefined) | ||
431 | { | ||
432 | if(active) | ||
433 | markerAddMap(marker); | ||
434 | else | ||
435 | markerRemoveMap(marker); | ||
436 | } | ||
437 | } | ||
438 | |||
439 | amb | 577 | var lonlat=map.getCenter().clone(); |
440 | amb | 1009 | lonlat.transform(epsg900913,epsg4326); |
441 | amb | 577 | |
442 | amb | 985 | if(routino.point[marker].lon!="") |
443 | amb | 577 | { |
444 | amb | 985 | if(routino.point[marker].lon<-180) routino.point[marker].lon=-180; |
445 | if(routino.point[marker].lon>+180) routino.point[marker].lon=+180; | ||
446 | lonlat.lon=routino.point[marker].lon; | ||
447 | amb | 577 | } |
448 | |||
449 | amb | 985 | if(routino.point[marker].lat!="") |
450 | amb | 577 | { |
451 | amb | 985 | if(routino.point[marker].lat<-90 ) routino.point[marker].lat=-90 ; |
452 | if(routino.point[marker].lat>+90 ) routino.point[marker].lat=+90 ; | ||
453 | lonlat.lat=routino.point[marker].lat; | ||
454 | amb | 577 | } |
455 | |||
456 | amb | 1009 | lonlat=lonlat.clone() |
457 | lonlat.transform(epsg4326,epsg900913); | ||
458 | amb | 577 | |
459 | amb | 1009 | markers[marker].move(lonlat); |
460 | amb | 577 | |
461 | markersmoved=true; | ||
462 | } | ||
463 | |||
464 | |||
465 | // | ||
466 | amb | 1001 | // Set the feature coordinates from the form when the form changes. |
467 | // | ||
468 | |||
469 | function formSetSearch(marker,search) | ||
470 | { | ||
471 | amb | 1005 | clearSearchResult(marker); |
472 | |||
473 | amb | 1001 | if(search == undefined) |
474 | { | ||
475 | routino.point[marker].search=document.forms["form"].elements["search" + marker].value; | ||
476 | |||
477 | DoSearch(marker); | ||
478 | } | ||
479 | else | ||
480 | { | ||
481 | document.forms["form"].elements["search" + marker].value=search; | ||
482 | |||
483 | routino.point[marker].search=search; | ||
484 | } | ||
485 | } | ||
486 | |||
487 | |||
488 | // | ||
489 | amb | 577 | // Format a number in printf("%.5f") format. |
490 | // | ||
491 | |||
492 | function format5f(number) | ||
493 | { | ||
494 | var newnumber=Math.floor(number*100000+0.5); | ||
495 | var delta=0; | ||
496 | |||
497 | if(newnumber>=0 && newnumber<100000) delta= 100000; | ||
498 | if(newnumber<0 && newnumber>-100000) delta=-100000; | ||
499 | |||
500 | var string=String(newnumber+delta); | ||
501 | |||
502 | var intpart =string.substring(0,string.length-5); | ||
503 | var fracpart=string.substring(string.length-5,string.length); | ||
504 | |||
505 | if(delta>0) intpart="0"; | ||
506 | if(delta<0) intpart="-0"; | ||
507 | |||
508 | return(intpart + "." + fracpart); | ||
509 | } | ||
510 | |||
511 | |||
512 | // | ||
513 | // Build a set of URL arguments | ||
514 | // | ||
515 | |||
516 | amb | 986 | function buildURLArguments(lang) |
517 | amb | 577 | { |
518 | amb | 986 | var url= "transport=" + routino.transport; |
519 | amb | 577 | |
520 | for(var marker=1;marker<=vismarkers;marker++) | ||
521 | amb | 986 | if(routino.point[marker].active) |
522 | amb | 577 | { |
523 | amb | 985 | url=url + ";lon" + marker + "=" + routino.point[marker].lon; |
524 | url=url + ";lat" + marker + "=" + routino.point[marker].lat; | ||
525 | amb | 1001 | if(routino.point[marker].search != "") |
526 | url=url + ";search" + marker + "=" + encodeURIComponent(routino.point[marker].search); | ||
527 | amb | 577 | } |
528 | |||
529 | for(var key in routino.profile_highway) | ||
530 | if(routino.profile_highway[key][routino.transport]!=routino_default.profile_highway[key][routino.transport]) | ||
531 | url=url + ";highway-" + key + "=" + routino.profile_highway[key][routino.transport]; | ||
532 | |||
533 | for(var key in routino.profile_speed) | ||
534 | if(routino.profile_speed[key][routino.transport]!=routino_default.profile_speed[key][routino.transport]) | ||
535 | url=url + ";speed-" + key + "=" + routino.profile_speed[key][routino.transport]; | ||
536 | |||
537 | for(var key in routino.profile_property) | ||
538 | if(routino.profile_property[key][routino.transport]!=routino_default.profile_property[key][routino.transport]) | ||
539 | url=url + ";property-" + key + "=" + routino.profile_property[key][routino.transport]; | ||
540 | |||
541 | for(var key in routino.restrictions) | ||
542 | if(routino.profile_restrictions[key][routino.transport]!=routino_default.profile_restrictions[key][routino.transport]) | ||
543 | url=url + ";" + key + "=" + routino.profile_restrictions[key][routino.transport]; | ||
544 | |||
545 | amb | 986 | if(lang && routino.language) |
546 | amb | 577 | url=url + ";language=" + routino.language; |
547 | |||
548 | return(url); | ||
549 | } | ||
550 | |||
551 | |||
552 | // | ||
553 | amb | 986 | // Build a set of URL arguments for the map location |
554 | amb | 569 | // |
555 | |||
556 | amb | 986 | function buildMapArguments() |
557 | amb | 569 | { |
558 | amb | 1009 | var lonlat = map.getCenter().clone(); |
559 | lonlat.transform(epsg900913,epsg4326); | ||
560 | amb | 569 | |
561 | amb | 986 | var zoom = map.getZoom() + map.minZoomLevel; |
562 | |||
563 | return "lat=" + format5f(lonlat.lat) + ";lon=" + format5f(lonlat.lon) + ";zoom=" + zoom; | ||
564 | amb | 569 | } |
565 | |||
566 | |||
567 | // | ||
568 | amb | 986 | // Update a URL |
569 | // | ||
570 | |||
571 | function updateURL(element) | ||
572 | { | ||
573 | if(element.id == "permalink_url") | ||
574 | element.href=location.pathname + "?" + buildURLArguments(true) + ";" + buildMapArguments(); | ||
575 | |||
576 | if(element.id == "visualiser_url") | ||
577 | element.href="visualiser.html" + "?" + buildMapArguments(); | ||
578 | |||
579 | if(element.id == "edit_url") | ||
580 | element.href="http://www.openstreetmap.org/edit" + "?" + buildMapArguments(); | ||
581 | |||
582 | if(element.id.match(/^lang_([a-zA-Z-]+)_url$/)) | ||
583 | element.href="router.html" + "." + RegExp.$1 + "?" + buildURLArguments(false) + ";" + buildMapArguments(); | ||
584 | } | ||
585 | |||
586 | |||
587 | amb | 577 | //////////////////////////////////////////////////////////////////////////////// |
588 | ///////////////////////////////// Map handling ///////////////////////////////// | ||
589 | //////////////////////////////////////////////////////////////////////////////// | ||
590 | amb | 569 | |
591 | var map; | ||
592 | amb | 933 | var layerMap=[], layerVectors, layerGPX; |
593 | amb | 569 | var epsg4326, epsg900913; |
594 | |||
595 | amb | 985 | // |
596 | amb | 569 | // Initialise the 'map' object |
597 | // | ||
598 | |||
599 | amb | 985 | function map_init() |
600 | amb | 569 | { |
601 | amb | 985 | lon =args["lon"]; |
602 | lat =args["lat"]; | ||
603 | zoom=args["zoom"]; | ||
604 | |||
605 | amb | 935 | // Map properties (North/South and East/West limits and zoom in/out limits) are now in mapprops.js |
606 | amb | 933 | // Map URLs are now in mapprops.js |
607 | amb | 577 | |
608 | amb | 569 | // |
609 | // Create the map | ||
610 | // | ||
611 | |||
612 | epsg4326=new OpenLayers.Projection("EPSG:4326"); | ||
613 | epsg900913=new OpenLayers.Projection("EPSG:900913"); | ||
614 | |||
615 | map = new OpenLayers.Map ("map", | ||
616 | { | ||
617 | controls:[ | ||
618 | new OpenLayers.Control.Navigation(), | ||
619 | new OpenLayers.Control.PanZoomBar(), | ||
620 | new OpenLayers.Control.ScaleLine(), | ||
621 | new OpenLayers.Control.LayerSwitcher() | ||
622 | ], | ||
623 | |||
624 | projection: epsg900913, | ||
625 | displayProjection: epsg4326, | ||
626 | |||
627 | amb | 933 | minZoomLevel: mapprops.zoomout, |
628 | numZoomLevels: mapprops.zoomin-mapprops.zoomout+1, | ||
629 | maxResolution: 156543.0339 / Math.pow(2,mapprops.zoomout), | ||
630 | amb | 569 | |
631 | amb | 577 | maxExtent: new OpenLayers.Bounds(-20037508.34, -20037508.34, 20037508.34, 20037508.34), |
632 | amb | 933 | restrictedExtent: new OpenLayers.Bounds(mapprops.westedge,mapprops.southedge,mapprops.eastedge,mapprops.northedge).transform(epsg4326,epsg900913), |
633 | amb | 569 | |
634 | units: "m" | ||
635 | }); | ||
636 | |||
637 | amb | 933 | // Add map tile layers |
638 | amb | 569 | |
639 | amb | 933 | for(var l=0;l < mapprops.mapdata.length;l++) |
640 | { | ||
641 | layerMap[l] = new OpenLayers.Layer.TMS(mapprops.mapdata[l].label, | ||
642 | mapprops.mapdata[l].baseurl, | ||
643 | { | ||
644 | emptyUrl: mapprops.mapdata[l].errorurl, | ||
645 | type: 'png', | ||
646 | getURL: limitedUrl, | ||
647 | displayOutsideMaxExtent: true, | ||
648 | buffer: 1 | ||
649 | }); | ||
650 | map.addLayer(layerMap[l]); | ||
651 | } | ||
652 | amb | 569 | |
653 | amb | 577 | // Get a URL for the tile; limited to map restricted extent. |
654 | amb | 569 | |
655 | function limitedUrl(bounds) | ||
656 | { | ||
657 | var z = map.getZoom() + map.minZoomLevel; | ||
658 | |||
659 | amb | 577 | if (z>=7 && (bounds.right < map.restrictedExtent.left || |
660 | bounds.left > map.restrictedExtent.right || | ||
661 | bounds.top < map.restrictedExtent.bottom || | ||
662 | bounds.bottom > map.restrictedExtent.top)) | ||
663 | amb | 569 | return this.emptyUrl; |
664 | |||
665 | var res = map.getResolution(); | ||
666 | var y = Math.round((this.maxExtent.top - bounds.top) / (res * this.tileSize.h)); | ||
667 | var limit = Math.pow(2, z); | ||
668 | |||
669 | if (y < 0 || y >= limit) | ||
670 | return this.emptyUrl; | ||
671 | |||
672 | var x = Math.round((bounds.left - this.maxExtent.left) / (res * this.tileSize.w)); | ||
673 | |||
674 | x = ((x % limit) + limit) % limit; | ||
675 | return this.url + z + "/" + x + "/" + y + "." + this.type; | ||
676 | } | ||
677 | |||
678 | // Define a GPX layer but don't add it yet | ||
679 | |||
680 | layerGPX={shortest: null, quickest: null}; | ||
681 | |||
682 | gpx_style={shortest: new OpenLayers.Style({},{strokeWidth: 3, strokeColor: "#00FF00"}), | ||
683 | quickest: new OpenLayers.Style({},{strokeWidth: 3, strokeColor: "#0000FF"})}; | ||
684 | amb | 985 | |
685 | amb | 569 | // Add a vectors layer |
686 | amb | 985 | |
687 | amb | 569 | layerVectors = new OpenLayers.Layer.Vector("Markers"); |
688 | map.addLayer(layerVectors); | ||
689 | |||
690 | // A set of markers | ||
691 | |||
692 | amb | 574 | markers={}; |
693 | amb | 569 | markersmoved=false; |
694 | paramschanged=false; | ||
695 | |||
696 | amb | 985 | for(var marker=1;marker<=maxmarkers;marker++) |
697 | amb | 569 | { |
698 | amb | 987 | markers[marker] = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(0,0),{}, |
699 | new OpenLayers.Style({},{externalGraphic: 'icons/marker-' + marker + '-red.png', | ||
700 | fillColor: "white", | ||
701 | graphicYOffset: -25, | ||
702 | graphicWidth: 21, | ||
703 | graphicHeight: 25, | ||
704 | display: "none"})); | ||
705 | amb | 569 | |
706 | amb | 987 | layerVectors.addFeatures([markers[marker]]); |
707 | amb | 569 | } |
708 | |||
709 | // A function to drag the markers | ||
710 | |||
711 | var drag = new OpenLayers.Control.DragFeature(layerVectors, | ||
712 | {onDrag: dragMove, | ||
713 | onComplete: dragComplete }); | ||
714 | map.addControl(drag); | ||
715 | drag.activate(); | ||
716 | |||
717 | // Markers to highlight a selected point | ||
718 | |||
719 | amb | 577 | for(var highlight in highlights) |
720 | amb | 569 | { |
721 | highlights[highlight] = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(0,0),{}, | ||
722 | amb | 577 | new OpenLayers.Style({},{strokeColor: route_dark_colours[highlight], |
723 | amb | 574 | fillColor: "white", |
724 | amb | 569 | pointRadius: 10, |
725 | strokeWidth: 4, | ||
726 | fillOpacity: 0, | ||
727 | display: "none"})); | ||
728 | |||
729 | layerVectors.addFeatures([highlights[highlight]]); | ||
730 | } | ||
731 | |||
732 | amb | 574 | // A popup for routing results |
733 | |||
734 | amb | 577 | for(var popup in popups) |
735 | popups[popup] = createPopup(popup); | ||
736 | amb | 574 | |
737 | amb | 572 | // Set the map centre to the limited range specified |
738 | |||
739 | amb | 577 | map.setCenter(map.restrictedExtent.getCenterLonLat(), map.getZoomForExtent(map.restrictedExtent,true)); |
740 | amb | 572 | map.maxResolution = map.getResolution(); |
741 | |||
742 | amb | 569 | // Move the map |
743 | |||
744 | amb | 985 | if(lon != undefined && lat != undefined && zoom != undefined) |
745 | amb | 569 | { |
746 | amb | 933 | if(lon<mapprops.westedge) lon=mapprops.westedge; |
747 | if(lon>mapprops.eastedge) lon=mapprops.eastedge; | ||
748 | |||
749 | if(lat<mapprops.southedge) lat=mapprops.southedge; | ||
750 | if(lat>mapprops.northedge) lat=mapprops.northedge; | ||
751 | |||
752 | if(zoom<mapprops.zoomout) zoom=mapprops.zoomout; | ||
753 | if(zoom>mapprops.zoomin) zoom=mapprops.zoomin; | ||
754 | |||
755 | amb | 1009 | var lonlat = new OpenLayers.LonLat(lon,lat); |
756 | lonlat.transform(epsg4326,epsg900913); | ||
757 | amb | 569 | |
758 | map.moveTo(lonlat,zoom-map.minZoomLevel); | ||
759 | } | ||
760 | } | ||
761 | |||
762 | |||
763 | // | ||
764 | // OpenLayers.Control.DragFeature callback for a drag occuring. | ||
765 | // | ||
766 | |||
767 | function dragMove(feature,pixel) | ||
768 | { | ||
769 | amb | 574 | for(var marker in markers) |
770 | amb | 569 | if(feature==markers[marker]) |
771 | { | ||
772 | markersmoved=true; | ||
773 | |||
774 | amb | 985 | dragSetForm(marker); |
775 | amb | 569 | } |
776 | } | ||
777 | |||
778 | |||
779 | // | ||
780 | // OpenLayers.Control.DragFeature callback for completing a drag. | ||
781 | // | ||
782 | |||
783 | function dragComplete(feature,pixel) | ||
784 | { | ||
785 | amb | 574 | for(var marker in markers) |
786 | amb | 569 | if(feature==markers[marker]) |
787 | { | ||
788 | markersmoved=true; | ||
789 | |||
790 | amb | 985 | dragSetForm(marker); |
791 | amb | 569 | } |
792 | } | ||
793 | |||
794 | |||
795 | amb | 985 | // |
796 | // Set the feature coordinates in the form after dragging. | ||
797 | // | ||
798 | |||
799 | function dragSetForm(marker) | ||
800 | { | ||
801 | var lonlat = new OpenLayers.LonLat(markers[marker].geometry.x, markers[marker].geometry.y); | ||
802 | amb | 1009 | lonlat.transform(epsg900913,epsg4326); |
803 | amb | 985 | |
804 | var lon=format5f(lonlat.lon); | ||
805 | var lat=format5f(lonlat.lat); | ||
806 | |||
807 | formSetCoords(marker,lon,lat); | ||
808 | } | ||
809 | |||
810 | |||
811 | amb | 577 | //////////////////////////////////////////////////////////////////////////////// |
812 | /////////////////////////////// Marker handling //////////////////////////////// | ||
813 | //////////////////////////////////////////////////////////////////////////////// | ||
814 | |||
815 | |||
816 | amb | 569 | // |
817 | amb | 574 | // Toggle a marker on the map. |
818 | amb | 569 | // |
819 | |||
820 | amb | 574 | function markerToggleMap(marker) |
821 | amb | 569 | { |
822 | amb | 1005 | clearSearchResult(marker); |
823 | |||
824 | amb | 985 | if(routino.point[marker].active) |
825 | amb | 574 | markerRemoveMap(marker); |
826 | else | ||
827 | markerAddMap(marker); | ||
828 | } | ||
829 | |||
830 | |||
831 | // | ||
832 | // Show a marker on the map. | ||
833 | // | ||
834 | |||
835 | function markerAddMap(marker) | ||
836 | { | ||
837 | amb | 1005 | clearSearchResult(marker); |
838 | |||
839 | amb | 577 | markers[marker].style.display = ""; |
840 | amb | 985 | routino.point[marker].active=true; |
841 | amb | 574 | |
842 | amb | 577 | updateIcon(marker); |
843 | amb | 574 | |
844 | markersmoved=true; | ||
845 | } | ||
846 | |||
847 | |||
848 | // | ||
849 | // Remove a marker from the map. | ||
850 | // | ||
851 | |||
852 | function markerRemoveMap(marker) | ||
853 | { | ||
854 | amb | 1005 | clearSearchResult(marker); |
855 | |||
856 | amb | 577 | markers[marker].style.display = "none"; |
857 | amb | 985 | routino.point[marker].active=false; |
858 | amb | 574 | |
859 | amb | 577 | updateIcon(marker); |
860 | amb | 574 | |
861 | amb | 577 | markersmoved=true; |
862 | } | ||
863 | amb | 574 | |
864 | amb | 577 | |
865 | // | ||
866 | amb | 1001 | // Display search string for the marker |
867 | // | ||
868 | |||
869 | function markerSearch(marker) | ||
870 | { | ||
871 | amb | 1005 | clearSearchResult(marker); |
872 | |||
873 | amb | 1011 | document.getElementById("coords" + marker).style.display="none"; |
874 | document.getElementById("search" + marker).style.display=""; | ||
875 | amb | 1001 | } |
876 | |||
877 | |||
878 | // | ||
879 | // Display coordinates for the marker | ||
880 | // | ||
881 | |||
882 | function markerCoords(marker) | ||
883 | { | ||
884 | amb | 1005 | clearSearchResult(marker); |
885 | |||
886 | amb | 1011 | document.getElementById("search" + marker).style.display="none"; |
887 | document.getElementById("coords" + marker).style.display=""; | ||
888 | amb | 1001 | } |
889 | |||
890 | |||
891 | // | ||
892 | amb | 577 | // Centre the marker on the map |
893 | // | ||
894 | |||
895 | function markerCentre(marker) | ||
896 | { | ||
897 | amb | 1005 | clearSearchResult(marker); |
898 | |||
899 | amb | 985 | var lonlat=map.getCenter().clone(); |
900 | amb | 1009 | lonlat.transform(epsg900913,epsg4326); |
901 | amb | 577 | |
902 | amb | 985 | formSetCoords(marker,lonlat.lon,lonlat.lat,true); |
903 | amb | 574 | } |
904 | |||
905 | |||
906 | // | ||
907 | amb | 984 | // Centre the map on the marker |
908 | // | ||
909 | |||
910 | function markerRecentre(marker) | ||
911 | { | ||
912 | amb | 1005 | clearSearchResult(marker); |
913 | |||
914 | amb | 985 | lon=routino.point[marker].lon; |
915 | lat=routino.point[marker].lat; | ||
916 | amb | 984 | |
917 | amb | 1009 | var lonlat = new OpenLayers.LonLat(lon,lat); |
918 | lonlat.transform(epsg4326,epsg900913); | ||
919 | amb | 984 | |
920 | map.panTo(lonlat); | ||
921 | } | ||
922 | |||
923 | |||
924 | // | ||
925 | amb | 574 | // Clear the current marker. |
926 | // | ||
927 | |||
928 | function markerRemove(marker) | ||
929 | { | ||
930 | amb | 1005 | clearSearchResult(marker); |
931 | |||
932 | amb | 574 | for(var marker2=marker;marker2<vismarkers;marker2++) |
933 | amb | 985 | formSetCoords(marker2,routino.point[marker2+1].lon,routino.point[marker2+1].lat,routino.point[marker2+1].active); |
934 | amb | 574 | |
935 | markerRemoveMap(vismarkers); | ||
936 | |||
937 | amb | 1011 | document.getElementById("waypoint" + vismarkers).style.display="none"; |
938 | amb | 574 | |
939 | vismarkers--; | ||
940 | |||
941 | if(vismarkers==1) | ||
942 | markerAddAfter(1); | ||
943 | } | ||
944 | |||
945 | |||
946 | // | ||
947 | // Add a marker before the current one. | ||
948 | // | ||
949 | |||
950 | function markerAddBefore(marker) | ||
951 | { | ||
952 | amb | 1005 | clearSearchResult(marker); |
953 | |||
954 | amb | 985 | if(vismarkers==maxmarkers || marker==1) |
955 | amb | 574 | return false; |
956 | |||
957 | vismarkers++; | ||
958 | |||
959 | amb | 1011 | document.getElementById("waypoint" + vismarkers).style.display=""; |
960 | amb | 574 | |
961 | for(var marker2=vismarkers;marker2>marker;marker2--) | ||
962 | amb | 985 | formSetCoords(marker2,routino.point[marker2-1].lon,routino.point[marker2-1].lat,routino.point[marker2-1].active); |
963 | amb | 569 | |
964 | amb | 985 | formSetCoords(marker,"","",false); |
965 | amb | 569 | |
966 | amb | 574 | markerRemoveMap(marker); |
967 | } | ||
968 | |||
969 | |||
970 | // | ||
971 | // Add a marker after the current one. | ||
972 | // | ||
973 | |||
974 | function markerAddAfter(marker) | ||
975 | { | ||
976 | amb | 1005 | clearSearchResult(marker); |
977 | |||
978 | amb | 985 | if(vismarkers==maxmarkers) |
979 | amb | 574 | return false; |
980 | |||
981 | vismarkers++; | ||
982 | |||
983 | amb | 1011 | document.getElementById("waypoint" + vismarkers).style.display=""; |
984 | amb | 574 | |
985 | for(var marker2=vismarkers;marker2>(marker+1);marker2--) | ||
986 | amb | 985 | formSetCoords(marker2,routino.point[marker2-1].lon,routino.point[marker2-1].lat,routino.point[marker2-1].active); |
987 | amb | 574 | |
988 | amb | 985 | formSetCoords(marker+1,"","",false); |
989 | amb | 569 | |
990 | amb | 574 | markerRemoveMap(marker+1); |
991 | amb | 569 | } |
992 | |||
993 | |||
994 | // | ||
995 | amb | 577 | // Set this marker as the home location. |
996 | // | ||
997 | |||
998 | function markerHome(marker) | ||
999 | { | ||
1000 | amb | 1005 | clearSearchResult(marker); |
1001 | |||
1002 | amb | 577 | if(markerHomeCookie(marker)) |
1003 | amb | 985 | for(marker=1;marker<=maxmarkers;marker++) |
1004 | amb | 577 | updateIcon(marker); |
1005 | } | ||
1006 | |||
1007 | |||
1008 | // | ||
1009 | amb | 984 | // Set this marker as the current location. |
1010 | // | ||
1011 | |||
1012 | function markerLocate(marker) | ||
1013 | { | ||
1014 | amb | 1005 | clearSearchResult(marker); |
1015 | |||
1016 | amb | 984 | if(navigator.geolocation) |
1017 | navigator.geolocation.getCurrentPosition( | ||
1018 | function(position) { | ||
1019 | amb | 985 | formSetCoords(marker,position.coords.longitude,position.coords.latitude,true); |
1020 | amb | 984 | }); |
1021 | } | ||
1022 | |||
1023 | |||
1024 | // | ||
1025 | amb | 577 | // Update an icon to set colours and home or normal marker. |
1026 | // | ||
1027 | |||
1028 | function updateIcon(marker) | ||
1029 | { | ||
1030 | amb | 985 | var lon=routino.point[marker].lon; |
1031 | var lat=routino.point[marker].lat; | ||
1032 | amb | 577 | |
1033 | if(lon==homelon && lat==homelat) | ||
1034 | { | ||
1035 | amb | 985 | if(routino.point[marker].active) |
1036 | amb | 577 | document.images["waypoint" + marker].src="icons/marker-home-red.png"; |
1037 | else | ||
1038 | document.images["waypoint" + marker].src="icons/marker-home-grey.png"; | ||
1039 | |||
1040 | markers[marker].style.externalGraphic="icons/marker-home-red.png"; | ||
1041 | } | ||
1042 | else | ||
1043 | { | ||
1044 | amb | 985 | if(routino.point[marker].active) |
1045 | amb | 577 | document.images["waypoint" + marker].src="icons/marker-" + marker + "-red.png"; |
1046 | else | ||
1047 | document.images["waypoint" + marker].src="icons/marker-" + marker + "-grey.png"; | ||
1048 | |||
1049 | markers[marker].style.externalGraphic="icons/marker-" + marker + "-red.png"; | ||
1050 | } | ||
1051 | |||
1052 | layerVectors.drawFeature(markers[marker]); | ||
1053 | } | ||
1054 | |||
1055 | |||
1056 | // | ||
1057 | // Set or clear the home marker icon | ||
1058 | // | ||
1059 | |||
1060 | function markerHomeCookie(marker) | ||
1061 | { | ||
1062 | amb | 985 | var lon=routino.point[marker].lon; |
1063 | var lat=routino.point[marker].lat; | ||
1064 | amb | 577 | |
1065 | if(lon=="" || lat=="") | ||
1066 | return(false); | ||
1067 | |||
1068 | var cookie; | ||
1069 | var date = new Date(); | ||
1070 | |||
1071 | if((homelat==null && homelon==null) || | ||
1072 | (homelat!=lat && homelon!=lon)) | ||
1073 | { | ||
1074 | cookie="Routino-home=lon:" + lon + ":lat:" + lat; | ||
1075 | |||
1076 | date.setUTCFullYear(date.getUTCFullYear()+5); | ||
1077 | |||
1078 | homelat=lat; | ||
1079 | homelon=lon; | ||
1080 | } | ||
1081 | else | ||
1082 | { | ||
1083 | cookie="Routino-home=unset"; | ||
1084 | |||
1085 | date.setUTCFullYear(date.getUTCFullYear()-1); | ||
1086 | |||
1087 | homelat=null; | ||
1088 | homelon=null; | ||
1089 | } | ||
1090 | |||
1091 | document.cookie=cookie + ";expires=" + date.toGMTString(); | ||
1092 | |||
1093 | return(true); | ||
1094 | } | ||
1095 | |||
1096 | |||
1097 | // | ||
1098 | amb | 574 | // Move this marker up. |
1099 | // | ||
1100 | |||
1101 | function markerMoveUp(marker) | ||
1102 | { | ||
1103 | if(marker==1) | ||
1104 | amb | 984 | { |
1105 | for(var m=1;m<vismarkers;m++) | ||
1106 | markerSwap(m,m+1); | ||
1107 | } | ||
1108 | else | ||
1109 | markerSwap(marker,marker-1); | ||
1110 | amb | 574 | } |
1111 | |||
1112 | |||
1113 | // | ||
1114 | // Move this marker down. | ||
1115 | // | ||
1116 | |||
1117 | function markerMoveDown(marker) | ||
1118 | { | ||
1119 | if(marker==vismarkers) | ||
1120 | amb | 984 | { |
1121 | for(var m=vismarkers;m>1;m--) | ||
1122 | markerSwap(m,m-1); | ||
1123 | } | ||
1124 | else | ||
1125 | markerSwap(marker,marker+1); | ||
1126 | amb | 574 | } |
1127 | |||
1128 | |||
1129 | // | ||
1130 | // Swap a pair of markers. | ||
1131 | // | ||
1132 | |||
1133 | function markerSwap(marker1,marker2) | ||
1134 | { | ||
1135 | amb | 985 | var lon=routino.point[marker1].lon; |
1136 | var lat=routino.point[marker1].lat; | ||
1137 | var active=routino.point[marker1].active; | ||
1138 | amb | 574 | |
1139 | amb | 985 | formSetCoords(marker1,routino.point[marker2].lon,routino.point[marker2].lat,routino.point[marker2].active); |
1140 | amb | 574 | |
1141 | amb | 985 | formSetCoords(marker2,lon,lat,active); |
1142 | amb | 574 | } |
1143 | |||
1144 | |||
1145 | // | ||
1146 | // Reverse the markers. | ||
1147 | // | ||
1148 | |||
1149 | function markersReverse() | ||
1150 | { | ||
1151 | for(var marker=1;marker<=vismarkers/2;marker++) | ||
1152 | markerSwap(marker,vismarkers+1-marker); | ||
1153 | } | ||
1154 | |||
1155 | |||
1156 | amb | 577 | //////////////////////////////////////////////////////////////////////////////// |
1157 | //////////////////////////// Route results handling //////////////////////////// | ||
1158 | //////////////////////////////////////////////////////////////////////////////// | ||
1159 | |||
1160 | var route_light_colours={shortest: "#60C060", quickest: "#6060C0"}; | ||
1161 | var route_dark_colours ={shortest: "#408040", quickest: "#404080"}; | ||
1162 | |||
1163 | var highlights={shortest: null, quickest: null}; | ||
1164 | var popups={shortest: null, quickest: null}; | ||
1165 | var routepoints={shortest: {}, quickest: {}}; | ||
1166 | var gpx_style={shortest: null, quickest: null}; | ||
1167 | |||
1168 | amb | 574 | // |
1169 | amb | 577 | // Zoom to a specific item in the route |
1170 | amb | 569 | // |
1171 | |||
1172 | amb | 577 | function zoomTo(type,line) |
1173 | amb | 569 | { |
1174 | amb | 1009 | var lonlat = new OpenLayers.LonLat(routepoints[type][line].lon,routepoints[type][line].lat); |
1175 | lonlat.transform(epsg4326,epsg900913); | ||
1176 | amb | 569 | |
1177 | amb | 577 | map.moveTo(lonlat,map.numZoomLevels-2); |
1178 | amb | 569 | } |
1179 | |||
1180 | |||
1181 | // | ||
1182 | amb | 577 | // Highlight a specific item in the route |
1183 | amb | 569 | // |
1184 | |||
1185 | amb | 577 | function highlight(type,line) |
1186 | amb | 569 | { |
1187 | amb | 577 | if(line==-1) |
1188 | { | ||
1189 | highlights[type].style.display = "none"; | ||
1190 | amb | 569 | |
1191 | amb | 577 | drawPopup(popups[type],null); |
1192 | amb | 569 | } |
1193 | amb | 577 | else |
1194 | amb | 569 | { |
1195 | amb | 577 | // Marker |
1196 | amb | 569 | |
1197 | amb | 1009 | var lonlat = new OpenLayers.LonLat(routepoints[type][line].lon,routepoints[type][line].lat); |
1198 | lonlat.transform(epsg4326,epsg900913); | ||
1199 | amb | 569 | |
1200 | amb | 577 | highlights[type].move(lonlat); |
1201 | amb | 569 | |
1202 | amb | 577 | if(highlights[type].style.display = "none") |
1203 | highlights[type].style.display = ""; | ||
1204 | amb | 569 | |
1205 | amb | 577 | // Popup |
1206 | amb | 569 | |
1207 | amb | 577 | drawPopup(popups[type],"<table>" + routepoints[type][line].html + "</table>"); |
1208 | } | ||
1209 | |||
1210 | layerVectors.drawFeature(highlights[type]); | ||
1211 | amb | 569 | } |
1212 | |||
1213 | |||
1214 | // | ||
1215 | amb | 577 | // Create a popup - not using OpenLayers because want it fixed on screen not fixed on map. |
1216 | amb | 569 | // |
1217 | |||
1218 | amb | 577 | function createPopup(type) |
1219 | amb | 569 | { |
1220 | amb | 577 | var popup=document.createElement('div'); |
1221 | amb | 569 | |
1222 | amb | 577 | popup.className = "popup"; |
1223 | amb | 569 | |
1224 | amb | 577 | popup.innerHTML = "<span></span>"; |
1225 | amb | 569 | |
1226 | amb | 577 | popup.style.display = "none"; |
1227 | amb | 569 | |
1228 | amb | 577 | popup.style.position = "fixed"; |
1229 | popup.style.top = "-4000px"; | ||
1230 | popup.style.left = "-4000px"; | ||
1231 | popup.style.zIndex = "100"; | ||
1232 | amb | 569 | |
1233 | amb | 577 | popup.style.padding = "5px"; |
1234 | amb | 569 | |
1235 | amb | 577 | popup.style.opacity=0.85; |
1236 | popup.style.backgroundColor=route_light_colours[type]; | ||
1237 | popup.style.border="4px solid " + route_dark_colours[type]; | ||
1238 | amb | 569 | |
1239 | amb | 577 | document.body.appendChild(popup); |
1240 | amb | 569 | |
1241 | amb | 577 | return(popup); |
1242 | amb | 569 | } |
1243 | |||
1244 | |||
1245 | // | ||
1246 | amb | 577 | // Draw a popup - not using OpenLayers because want it fixed on screen not fixed on map. |
1247 | amb | 569 | // |
1248 | |||
1249 | amb | 577 | function drawPopup(popup,html) |
1250 | amb | 569 | { |
1251 | amb | 577 | if(html==null) |
1252 | amb | 569 | { |
1253 | amb | 577 | popup.style.display="none"; |
1254 | return; | ||
1255 | } | ||
1256 | amb | 574 | |
1257 | amb | 577 | if(popup.style.display=="none") |
1258 | amb | 569 | { |
1259 | amb | 577 | var map_div=document.getElementById("map"); |
1260 | amb | 569 | |
1261 | amb | 577 | popup.style.left =map_div.offsetParent.offsetLeft+map_div.offsetLeft+60 + "px"; |
1262 | popup.style.top = map_div.offsetTop +30 + "px"; | ||
1263 | popup.style.width =map_div.clientWidth-100 + "px"; | ||
1264 | amb | 574 | |
1265 | amb | 577 | popup.style.display=""; |
1266 | amb | 569 | } |
1267 | |||
1268 | amb | 577 | popup.innerHTML=html; |
1269 | amb | 569 | } |
1270 | |||
1271 | |||
1272 | // | ||
1273 | amb | 577 | // Remove a GPX trace |
1274 | amb | 569 | // |
1275 | |||
1276 | amb | 577 | function removeGPXTrace(type) |
1277 | amb | 569 | { |
1278 | amb | 577 | map.removeLayer(layerGPX[type]); |
1279 | layerGPX[type].destroy(); | ||
1280 | layerGPX[type]=null; | ||
1281 | amb | 569 | |
1282 | amb | 577 | displayStatus(type,"no_info"); |
1283 | amb | 569 | |
1284 | amb | 1011 | document.getElementById(type + "_links").style.display = "none"; |
1285 | amb | 569 | |
1286 | amb | 1011 | document.getElementById(type + "_route").innerHTML = ""; |
1287 | amb | 569 | |
1288 | amb | 577 | hideshow_hide(type); |
1289 | } | ||
1290 | amb | 569 | |
1291 | amb | 574 | |
1292 | amb | 577 | //////////////////////////////////////////////////////////////////////////////// |
1293 | /////////////////////////////// Server handling //////////////////////////////// | ||
1294 | //////////////////////////////////////////////////////////////////////////////// | ||
1295 | amb | 569 | |
1296 | // | ||
1297 | // Display data statistics | ||
1298 | // | ||
1299 | |||
1300 | function displayStatistics() | ||
1301 | { | ||
1302 | // Use AJAX to get the statistics | ||
1303 | |||
1304 | OpenLayers.loadURL("statistics.cgi",null,null,runStatisticsSuccess); | ||
1305 | } | ||
1306 | |||
1307 | |||
1308 | // | ||
1309 | amb | 577 | // Success in running data statistics generation. |
1310 | amb | 569 | // |
1311 | |||
1312 | function runStatisticsSuccess(response) | ||
1313 | { | ||
1314 | amb | 1011 | document.getElementById("statistics_data").innerHTML="<pre>" + response.responseText + "</pre>"; |
1315 | document.getElementById("statistics_link").style.display="none"; | ||
1316 | amb | 569 | } |
1317 | |||
1318 | |||
1319 | // | ||
1320 | amb | 577 | // Submit form - perform the routing |
1321 | amb | 569 | // |
1322 | |||
1323 | function findRoute(type) | ||
1324 | { | ||
1325 | tab_select("results"); | ||
1326 | |||
1327 | amb | 572 | hideshow_hide('help_options'); |
1328 | amb | 569 | hideshow_hide('shortest'); |
1329 | hideshow_hide('quickest'); | ||
1330 | |||
1331 | amb | 577 | displayStatus("result","running"); |
1332 | amb | 569 | |
1333 | amb | 986 | var url="router.cgi" + "?" + buildURLArguments(true) + ";type=" + type; |
1334 | amb | 569 | |
1335 | // Destroy the existing layer(s) | ||
1336 | |||
1337 | if(markersmoved || paramschanged) | ||
1338 | { | ||
1339 | if(layerGPX.shortest!=null) | ||
1340 | removeGPXTrace("shortest"); | ||
1341 | if(layerGPX.quickest!=null) | ||
1342 | removeGPXTrace("quickest"); | ||
1343 | markersmoved=false; | ||
1344 | paramschanged=false; | ||
1345 | } | ||
1346 | else if(layerGPX[type]!=null) | ||
1347 | removeGPXTrace(type); | ||
1348 | |||
1349 | // Use AJAX to run the router | ||
1350 | |||
1351 | routing_type=type; | ||
1352 | |||
1353 | OpenLayers.loadURL(url,null,null,runRouterSuccess,runRouterFailure); | ||
1354 | } | ||
1355 | |||
1356 | |||
1357 | // | ||
1358 | // Success in running router. | ||
1359 | // | ||
1360 | |||
1361 | function runRouterSuccess(response) | ||
1362 | { | ||
1363 | var lines=response.responseText.split('\n'); | ||
1364 | |||
1365 | var uuid=lines[0]; | ||
1366 | amb | 935 | var cpuinfo=lines[1]; // not used |
1367 | var distinfo=lines[2]; // not used | ||
1368 | var message=lines[3]; // content not used | ||
1369 | amb | 569 | |
1370 | amb | 629 | var link; |
1371 | |||
1372 | amb | 569 | // Update the status message |
1373 | |||
1374 | if(message!="") | ||
1375 | { | ||
1376 | amb | 577 | displayStatus("result","error"); |
1377 | amb | 572 | hideshow_show('help_route'); |
1378 | amb | 629 | |
1379 | link=document.getElementById("router_log_error"); | ||
1380 | link.href="results.cgi?uuid=" + uuid + ";type=router;format=log"; | ||
1381 | |||
1382 | amb | 569 | return; |
1383 | } | ||
1384 | else | ||
1385 | { | ||
1386 | amb | 577 | displayStatus("result","complete"); |
1387 | amb | 572 | hideshow_hide('help_route'); |
1388 | amb | 629 | |
1389 | link=document.getElementById("router_log_complete"); | ||
1390 | link.href="results.cgi?uuid=" + uuid + ";type=router;format=log"; | ||
1391 | amb | 569 | } |
1392 | |||
1393 | // Update the routing result message | ||
1394 | |||
1395 | amb | 577 | link=document.getElementById(routing_type + "_html"); |
1396 | link.href="results.cgi?uuid=" + uuid + ";type=" + routing_type + ";format=html"; | ||
1397 | amb | 1011 | |
1398 | amb | 577 | link=document.getElementById(routing_type + "_gpx_track"); |
1399 | link.href="results.cgi?uuid=" + uuid + ";type=" + routing_type + ";format=gpx-track"; | ||
1400 | amb | 1011 | |
1401 | amb | 577 | link=document.getElementById(routing_type + "_gpx_route"); |
1402 | link.href="results.cgi?uuid=" + uuid + ";type=" + routing_type + ";format=gpx-route"; | ||
1403 | amb | 1011 | |
1404 | amb | 577 | link=document.getElementById(routing_type + "_text_all"); |
1405 | link.href="results.cgi?uuid=" + uuid + ";type=" + routing_type + ";format=text-all"; | ||
1406 | amb | 1011 | |
1407 | amb | 577 | link=document.getElementById(routing_type + "_text"); |
1408 | link.href="results.cgi?uuid=" + uuid + ";type=" + routing_type + ";format=text"; | ||
1409 | amb | 569 | |
1410 | amb | 1011 | links=document.getElementById(routing_type + "_links").style.display = ""; |
1411 | amb | 569 | |
1412 | // Add a GPX layer | ||
1413 | |||
1414 | var url="results.cgi?uuid=" + uuid + ";type=" + routing_type + ";format=gpx-track"; | ||
1415 | |||
1416 | layerGPX[routing_type] = new OpenLayers.Layer.GML("GPX (" + routing_type + ")", url, | ||
1417 | { | ||
1418 | format: OpenLayers.Format.GPX, | ||
1419 | style: gpx_style[routing_type], | ||
1420 | projection: map.displayProjection | ||
1421 | }); | ||
1422 | |||
1423 | map.addLayer(layerGPX[routing_type]); | ||
1424 | |||
1425 | hideshow_show(routing_type); | ||
1426 | |||
1427 | displayResult(routing_type,uuid); | ||
1428 | } | ||
1429 | |||
1430 | |||
1431 | // | ||
1432 | // Failure in running router. | ||
1433 | // | ||
1434 | |||
1435 | function runRouterFailure(response) | ||
1436 | { | ||
1437 | amb | 577 | displayStatus("result","failed"); |
1438 | amb | 569 | } |
1439 | |||
1440 | |||
1441 | // | ||
1442 | amb | 577 | // Display the status |
1443 | // | ||
1444 | |||
1445 | function displayStatus(type,subtype,content) | ||
1446 | { | ||
1447 | amb | 1011 | var child=document.getElementById(type + "_status").firstChild; |
1448 | amb | 577 | |
1449 | do | ||
1450 | { | ||
1451 | if(child.id != undefined) | ||
1452 | child.style.display="none"; | ||
1453 | |||
1454 | child=child.nextSibling; | ||
1455 | } | ||
1456 | while(child != undefined); | ||
1457 | |||
1458 | amb | 936 | var chosen_status=document.getElementById(type + "_status_" + subtype); |
1459 | amb | 577 | |
1460 | amb | 936 | chosen_status.style.display=""; |
1461 | amb | 577 | |
1462 | if(content != null) | ||
1463 | amb | 936 | chosen_status.innerHTML=content; |
1464 | amb | 577 | } |
1465 | |||
1466 | |||
1467 | // | ||
1468 | amb | 569 | // Display the route |
1469 | // | ||
1470 | |||
1471 | function displayResult(type,uuid) | ||
1472 | { | ||
1473 | routing_type = type; | ||
1474 | |||
1475 | // Add the route | ||
1476 | |||
1477 | amb | 577 | var url="results.cgi?uuid=" + uuid + ";type=" + routing_type + ";format=html"; |
1478 | amb | 569 | |
1479 | // Use AJAX to get the route | ||
1480 | |||
1481 | OpenLayers.loadURL(url,null,null,getRouteSuccess,getRouteFailure); | ||
1482 | } | ||
1483 | |||
1484 | |||
1485 | // | ||
1486 | // Success in getting route. | ||
1487 | // | ||
1488 | |||
1489 | function getRouteSuccess(response) | ||
1490 | { | ||
1491 | var lines=response.responseText.split('\n'); | ||
1492 | |||
1493 | amb | 577 | routepoints[routing_type]=[]; |
1494 | amb | 569 | |
1495 | amb | 577 | var points=routepoints[routing_type]; |
1496 | amb | 569 | |
1497 | amb | 577 | var table=0; |
1498 | var point=0; | ||
1499 | var total_table,total_word; | ||
1500 | amb | 569 | |
1501 | amb | 577 | for(var line=0;line<lines.length;line++) |
1502 | amb | 569 | { |
1503 | amb | 577 | var thisline=lines[line]; |
1504 | amb | 569 | |
1505 | amb | 577 | if(table==0) |
1506 | { | ||
1507 | if(thisline.match('<table>')) | ||
1508 | table=1; | ||
1509 | else | ||
1510 | continue; | ||
1511 | } | ||
1512 | amb | 574 | |
1513 | amb | 577 | if(thisline.match('</table>')) |
1514 | break; | ||
1515 | |||
1516 | if(thisline.match('<tr class=\'([a-z])\'>')) | ||
1517 | amb | 569 | { |
1518 | amb | 577 | var rowtype=RegExp.$1; |
1519 | amb | 569 | |
1520 | amb | 577 | if(rowtype=='c') |
1521 | { | ||
1522 | thisline.match('<td class=\'r\'> *([-0-9.]+) *([-0-9.]+)'); | ||
1523 | points[point]={lat: Number(RegExp.$1), lon: Number(RegExp.$2), html: "", highway: "", distance: "", total: ""}; | ||
1524 | amb | 574 | |
1525 | amb | 577 | point++; |
1526 | } | ||
1527 | else if(rowtype=='n') | ||
1528 | { | ||
1529 | points[point-1].html += thisline; | ||
1530 | } | ||
1531 | else if(rowtype=='s') | ||
1532 | { | ||
1533 | thisline.match('<span class=\'h\'>([^<]+)</span>'); | ||
1534 | points[point-1].highway = RegExp.$1; | ||
1535 | amb | 574 | |
1536 | amb | 577 | thisline.match('<span class=\'d\'>([^<]+)</span>'); |
1537 | points[point-1].distance = RegExp.$1; | ||
1538 | amb | 574 | |
1539 | amb | 577 | thisline.match('(<span class=\'j\'>[^<]+</span>)'); |
1540 | points[point-1].total = RegExp.$1; | ||
1541 | |||
1542 | thisline.match('^(.*).<span class=\'j\'>'); | ||
1543 | |||
1544 | points[point-1].html += RegExp.$1; | ||
1545 | } | ||
1546 | else if(rowtype=='t') | ||
1547 | { | ||
1548 | points[point-1].html += thisline; | ||
1549 | |||
1550 | thisline.match('^(.*<td class=\'r\'>)'); | ||
1551 | total_table = RegExp.$1; | ||
1552 | |||
1553 | thisline.match('<td class=\'l\'>([^<]+)<'); | ||
1554 | total_word = RegExp.$1; | ||
1555 | |||
1556 | thisline.match('<span class=\'j\'>([^<]+)</span>'); | ||
1557 | points[point-1].total = RegExp.$1; | ||
1558 | } | ||
1559 | amb | 569 | } |
1560 | } | ||
1561 | |||
1562 | amb | 935 | displayStatus(routing_type,"info",points[point-1].total.bold()); |
1563 | |||
1564 | amb | 577 | var result="<table onmouseout='highlight(\"" + routing_type + "\",-1)'>"; |
1565 | |||
1566 | for(var p=0;p<point-1;p++) | ||
1567 | { | ||
1568 | points[p].html += total_table + points[p].total; | ||
1569 | |||
1570 | result=result + "<tr onclick='zoomTo(\"" + routing_type + "\"," + p + ")'" + | ||
1571 | " onmouseover='highlight(\"" + routing_type + "\"," + p + ")'>" + | ||
1572 | "<td class='distance' title='" + points[p].distance + "'>#" + (p+1) + | ||
1573 | "<td class='highway'>" + points[p].highway; | ||
1574 | } | ||
1575 | |||
1576 | result=result + "<tr onclick='zoomTo(\"" + routing_type + "\"," + p + ")'" + | ||
1577 | " onmouseover='highlight(\"" + routing_type + "\"," + p + ")'>" + | ||
1578 | "<td colspan='2'>" + total_word + " " + points[p].total; | ||
1579 | |||
1580 | amb | 569 | result=result + "</table>"; |
1581 | |||
1582 | amb | 1011 | document.getElementById(routing_type + "_route").innerHTML=result; |
1583 | amb | 569 | } |
1584 | |||
1585 | |||
1586 | // | ||
1587 | // Failure in getting route. | ||
1588 | // | ||
1589 | |||
1590 | function getRouteFailure(response) | ||
1591 | { | ||
1592 | amb | 1011 | document.getElementById(routing_type + "_route").innerHTML = ""; |
1593 | amb | 569 | } |
1594 | amb | 1001 | |
1595 | |||
1596 | // | ||
1597 | // Perform a search | ||
1598 | // | ||
1599 | |||
1600 | function DoSearch(marker) | ||
1601 | { | ||
1602 | // Use AJAX to get the search result | ||
1603 | |||
1604 | var search=routino.point[marker].search; | ||
1605 | |||
1606 | amb | 1009 | var bounds=map.getExtent().clone(); |
1607 | bounds.transform(epsg900913,epsg4326); | ||
1608 | amb | 1001 | |
1609 | amb | 1008 | var url="search.cgi?marker=" + marker + |
1610 | amb | 1009 | ";left=" + format5f(bounds.left) + |
1611 | ";top=" + format5f(bounds.top) + | ||
1612 | ";right=" + format5f(bounds.right) + | ||
1613 | ";bottom=" + format5f(bounds.bottom) + | ||
1614 | amb | 1008 | ";search=" + encodeURIComponent(search); |
1615 | |||
1616 | amb | 1001 | OpenLayers.loadURL(url,null,null,runSearchSuccess); |
1617 | } | ||
1618 | |||
1619 | |||
1620 | amb | 1005 | var searchresults=[]; |
1621 | |||
1622 | amb | 1001 | // |
1623 | // Success in running search. | ||
1624 | // | ||
1625 | |||
1626 | function runSearchSuccess(response) | ||
1627 | { | ||
1628 | var lines=response.responseText.split('\n'); | ||
1629 | |||
1630 | var marker=lines[0]; | ||
1631 | var cpuinfo=lines[1]; // not used | ||
1632 | amb | 1005 | var message=lines[2]; // not used |
1633 | amb | 1001 | |
1634 | amb | 1005 | if(message != "") |
1635 | { | ||
1636 | alert(message); | ||
1637 | return; | ||
1638 | } | ||
1639 | amb | 1001 | |
1640 | amb | 1005 | searchresults[marker]=[]; |
1641 | |||
1642 | for(var line=3;line<lines.length;line++) | ||
1643 | { | ||
1644 | var thisline=lines[line]; | ||
1645 | |||
1646 | if(thisline=="") | ||
1647 | break; | ||
1648 | |||
1649 | thisline.match('([-.0-9]+) ([-.0-9]+) (.*)'); | ||
1650 | |||
1651 | searchresults[marker][searchresults[marker].length]={lat: Number(RegExp.$1), lon: Number(RegExp.$2), name: RegExp.$3}; | ||
1652 | } | ||
1653 | |||
1654 | if(searchresults[marker].length==1) | ||
1655 | { | ||
1656 | formSetSearch(marker,searchresults[marker][0].name); | ||
1657 | formSetCoords(marker,searchresults[marker][0].lon,searchresults[marker][0].lat,true); | ||
1658 | } | ||
1659 | else | ||
1660 | { | ||
1661 | var results=document.getElementById("searchresults" + marker); | ||
1662 | |||
1663 | var innerHTML="<td colspan=\"3\">"; | ||
1664 | |||
1665 | for(var n=0;n<searchresults[marker].length;n++) | ||
1666 | { | ||
1667 | if(n>0) | ||
1668 | innerHTML+="<br>"; | ||
1669 | |||
1670 | innerHTML+="<a href=\"#\" onclick=\"choseSearchResult(" + marker + "," + n + ")\">" + | ||
1671 | searchresults[marker][n].name + | ||
1672 | "</a>"; | ||
1673 | } | ||
1674 | |||
1675 | results.innerHTML=innerHTML; | ||
1676 | |||
1677 | results.style.display=""; | ||
1678 | } | ||
1679 | amb | 1001 | } |
1680 | amb | 1005 | |
1681 | |||
1682 | // | ||
1683 | // Display search results. | ||
1684 | // | ||
1685 | |||
1686 | function choseSearchResult(marker,n) | ||
1687 | { | ||
1688 | if(n>=0) | ||
1689 | { | ||
1690 | formSetSearch(marker,searchresults[marker][n].name); | ||
1691 | formSetCoords(marker,searchresults[marker][n].lon,searchresults[marker][n].lat,true); | ||
1692 | } | ||
1693 | } | ||
1694 | |||
1695 | |||
1696 | // | ||
1697 | // Clear search results. | ||
1698 | // | ||
1699 | |||
1700 | function clearSearchResult(marker) | ||
1701 | { | ||
1702 | amb | 1011 | document.getElementById("searchresults" + marker).style.display="none"; |
1703 | amb | 1005 | } |