Routino SVN Repository Browser

Check out the latest version of Routino: svn co http://routino.org/svn/trunk routino

ViewVC logotype

Annotation of /trunk/web/www/routino/router.js

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1043 - (hide annotations) (download) (as text)
Sat Aug 4 15:24:32 2012 UTC (12 years, 7 months ago) by amb
File MIME type: application/javascript
File size: 47025 byte(s)
Don't send back the unused lines from the router CGI, add the complete command
line and execution time to the log file.

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