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.openlayers.js

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1948 - (hide annotations) (download) (as text)
Fri Jun 15 18:14:43 2018 UTC (6 years, 9 months ago) by amb
File MIME type: application/javascript
File size: 56515 byte(s)
Enable the search buttons if two markers are placed by searching for a
named location.

1 amb 569 //
2     // Routino router web page Javascript
3     //
4     // Part of the Routino routing software.
5     //
6 amb 1948 // This file Copyright 2008-2018 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 amb 1483 if(typeof(routino[l1])!="object")
36 amb 577 routino_default[l1]=routino[l1];
37     else
38     {
39     routino_default[l1]={};
40     for(var l2 in routino[l1])
41 amb 1483 if(typeof(routino[l1][l2])!="object")
42 amb 577 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 amb 1813 "^language" : "^[-a-zA-Z]+$",
87 amb 985
88 amb 1813 "^reverse" : "(1|0|true|false|on|off)",
89     "^loop" : "(1|0|true|false|on|off)"};
90    
91 amb 985 var args={};
92    
93     if(location.search.length>1)
94     {
95     var query,queries;
96    
97     query=location.search.replace(/^\?/,"");
98 amb 1483 query=query.replace(/;/g,"&");
99     queries=query.split("&");
100 amb 985
101     for(var i=0;i<queries.length;i++)
102     {
103     queries[i].match(/^([^=]+)(=(.*))?$/);
104    
105 amb 1483 var k=RegExp.$1;
106 amb 1484 var v=decodeURIComponent(RegExp.$3);
107 amb 985
108     for(var l in legal)
109     {
110     if(k.match(RegExp(l)) && v.match(RegExp(legal[l])))
111     args[k]=v;
112     }
113     }
114     }
115    
116    
117 amb 987 //
118     // Fill in the HTML - add the missing waypoints
119     //
120    
121 amb 1035 function html_init() // called from router.html
122 amb 987 {
123     var waypoints=document.getElementById("waypoints");
124 amb 1005
125 amb 1656 var waypoint_html=waypoints.firstElementChild.outerHTML.split("XXX");
126 amb 987
127 amb 1656 waypoints.removeChild(waypoints.firstElementChild);
128 amb 987
129 amb 1656 for(var marker=1;marker<=mapprops.maxmarkers;marker++)
130 amb 987 {
131 amb 1656 var waypoint=document.createElement('div');
132 amb 1011
133 amb 1656 waypoints.appendChild(waypoint);
134 amb 987
135 amb 1656 waypoint.outerHTML=waypoint_html.join(marker);
136     }
137 amb 1005
138 amb 1656 waypoints.addEventListener('dragstart',dragWaypointStart,false);
139     waypoints.addEventListener('dragend' ,dragWaypointEnd ,false);
140     waypoints.addEventListener('dragenter',dragWaypointEnter,false);
141     waypoints.addEventListener('dragover' ,dragWaypointOver ,false);
142     waypoints.addEventListener('dragleave',dragWaypointLeave,false);
143     waypoints.addEventListener('drop' ,dragWaypointDrop ,false);
144    
145    
146     var map=document.getElementById("map");
147    
148     map.addEventListener('dragenter',dragWaypointMapEnter,false);
149     map.addEventListener('dragover' ,dragWaypointMapOver ,false);
150     map.addEventListener('dragleave',dragWaypointMapLeave,false);
151     map.addEventListener('drop' ,dragWaypointMapDrop ,false);
152 amb 987 }
153    
154    
155 amb 577 ////////////////////////////////////////////////////////////////////////////////
156     //////////////////////////////// Form handling /////////////////////////////////
157     ////////////////////////////////////////////////////////////////////////////////
158    
159 amb 569 //
160     // Form initialisation - fill in the uninitialised parts
161     //
162    
163 amb 1035 function form_init() // called from router.html
164 amb 569 {
165 amb 985 // Fill in the waypoints
166 amb 569
167 amb 1039 vismarkers=0;
168 amb 569
169 amb 1012 for(var marker=mapprops.maxmarkers;marker>=1;marker--)
170 amb 569 {
171 amb 985 var lon=args["lon" + marker];
172     var lat=args["lat" + marker];
173 amb 1001 var search=args["search" + marker];
174 amb 577
175 amb 1493 if(lon !== undefined && lat !== undefined && search !== undefined && lon !== "" && lat !== "" && search !== "")
176 amb 569 {
177 amb 1037 markerAddForm(marker);
178    
179 amb 1001 formSetSearch(marker,search);
180 amb 1032 formSetCoords(marker,lon,lat);
181 amb 569
182 amb 1032 markerAddMap(marker);
183    
184 amb 1001 markerSearch(marker);
185    
186 amb 1039 vismarkers++;
187 amb 569 }
188 amb 1493 else if(lon !== undefined && lat !== undefined && lon !== "" && lat !== "")
189 amb 1001 {
190 amb 1037 markerAddForm(marker);
191    
192 amb 1032 formSetCoords(marker,lon,lat);
193 amb 1001
194 amb 1032 markerAddMap(marker);
195    
196 amb 1001 markerCoords(marker);
197    
198 amb 1039 vismarkers++;
199 amb 1001 }
200 amb 1493 else if(search !== undefined && search !== "")
201 amb 1001 {
202 amb 1037 markerAddForm(marker);
203    
204 amb 1001 formSetSearch(marker,search);
205    
206     markerSearch(marker);
207    
208     DoSearch(marker);
209    
210 amb 1039 vismarkers++;
211 amb 1001 }
212 amb 1039 else if(vismarkers || marker<=2)
213     {
214 amb 1037 markerAddForm(marker);
215 amb 1016
216 amb 1039 vismarkers++;
217     }
218    
219 amb 1016 var searchfield=document.forms["form"].elements["search" + marker];
220    
221     if(searchfield.addEventListener)
222 amb 1483 searchfield.addEventListener("keyup", searchOnReturnKey, false);
223 amb 1016 else if(searchfield.attachEvent)
224 amb 1483 searchfield.attachEvent("keyup", searchOnReturnKey); // Internet Explorer
225 amb 985 }
226 amb 569
227 amb 1813 if(args["loop"] !== undefined)
228     formSetLoopReverse("loop",args["loop"]);
229     else
230     formSetLoopReverse("loop",false);
231    
232     if(args["reverse"] !== undefined)
233     formSetLoopReverse("reverse",args["reverse"]);
234     else
235     formSetLoopReverse("reverse",false);
236    
237 amb 985 // Update the transport type with the URL settings which updates all HTML forms to defaults.
238 amb 574
239 amb 985 var transport=routino.transport;
240 amb 569
241 amb 1493 if(args["transport"] !== undefined)
242 amb 985 transport=args["transport"];
243 amb 577
244 amb 985 formSetTransport(transport);
245 amb 577
246 amb 985 // Update the HTML with the URL settings
247    
248 amb 1493 if(args["language"] !== undefined)
249 amb 985 formSetLanguage(args["language"]);
250 amb 1893 else
251     formSetLanguage();
252 amb 985
253     for(var key in routino.profile_highway)
254 amb 1493 if(args["highway-" + key] !== undefined)
255 amb 985 formSetHighway(key,args["highway-" + key]);
256    
257     for(var key in routino.profile_speed)
258 amb 1493 if(args["speed-" + key] !== undefined)
259 amb 985 formSetSpeed(key,args["speed-" + key]);
260    
261     for(var key in routino.profile_property)
262 amb 1493 if(args["property-" + key] !== undefined)
263 amb 985 formSetProperty(key,args["property-" + key]);
264    
265     for(var key in routino.restrictions)
266 amb 574 {
267 amb 985 if(key=="oneway" || key=="turns")
268 amb 577 {
269 amb 1493 if(args[key] !== undefined)
270 amb 985 formSetRestriction(key,args[key]);
271 amb 577 }
272 amb 985 else
273     {
274 amb 1493 if(args["restrict-" + key] !== undefined)
275 amb 985 formSetRestriction(key,args["restrict-" + key]);
276     }
277 amb 574 }
278 amb 569
279 amb 577 // Get the home location cookie and compare to each waypoint
280    
281 amb 1483 var cookies=document.cookie.split("; ");
282 amb 577
283     for(var cookie=0;cookie<cookies.length;cookie++)
284     if(cookies[cookie].substr(0,"Routino-home".length)=="Routino-home")
285     {
286     var data=cookies[cookie].split(/[=:;]/);
287    
288     if(data[1]=="lon") homelon=Number(data[2]);
289     if(data[3]=="lat") homelat=Number(data[4]);
290     }
291    
292 amb 1493 if(homelon!==null && homelat!==null)
293 amb 577 {
294 amb 1039 for(var m=1;m<=vismarkers;m++)
295     markerCheckHome(m);
296 amb 577
297     // If the first location is empty and the cookie is set then fill it.
298    
299 amb 1034 if(!routino.point[1].used)
300 amb 1036 markerMoveHome(1);
301 amb 577 }
302 amb 1486
303     updateURLs();
304 amb 1548
305     updateSearchButtons();
306 amb 569 }
307    
308    
309     //
310 amb 1016 // Function to perform the search if the return key is pressed.
311     // (using 'onchange' only triggers once and is confusing when clicking outside the field).
312     //
313    
314     function searchOnReturnKey(ev)
315     {
316     if(ev.keyCode==13)
317     if(this.name.match(/^search([0-9]+)$/))
318     formSetSearch(RegExp.$1);
319    
320     return(true);
321     }
322    
323    
324     //
325 amb 577 // Change of language in the form
326     //
327    
328 amb 1035 function formSetLanguage(value) // called from router.html (with no arguments)
329 amb 577 {
330 amb 1493 if(value === undefined)
331 amb 985 {
332     for(var lang=0;lang<document.forms["form"].elements["language"].length;lang++)
333     if(document.forms["form"].elements["language"][lang].checked)
334     routino.language=document.forms["form"].elements["language"][lang].value;
335     }
336     else
337     {
338     for(var lang=0;lang<document.forms["form"].elements["language"].length;lang++)
339     if(document.forms["form"].elements["language"][lang].value==value)
340     document.forms["form"].elements["language"][lang].checked=true;
341     else
342     document.forms["form"].elements["language"][lang].checked=false;
343 amb 577
344 amb 985 routino.language=value;
345     }
346 amb 1486
347     updateURLs();
348 amb 577 }
349    
350    
351     //
352 amb 569 // Change of transport in the form
353     //
354    
355 amb 1035 function formSetTransport(value) // called from router.html
356 amb 569 {
357 amb 985 routino.transport=value;
358 amb 569
359 amb 577 for(var key in routino.transports)
360     document.forms["form"].elements["transport"][routino.transports[key]-1].checked=(key==routino.transport);
361 amb 569
362 amb 577 for(var key in routino.profile_highway)
363     document.forms["form"].elements["highway-" + key].value=routino.profile_highway[key][routino.transport];
364 amb 569
365 amb 577 for(var key in routino.profile_speed)
366     document.forms["form"].elements["speed-" + key].value=routino.profile_speed[key][routino.transport];
367 amb 569
368 amb 577 for(var key in routino.profile_property)
369     document.forms["form"].elements["property-" + key].value=routino.profile_property[key][routino.transport];
370 amb 569
371 amb 577 for(var key in routino.restrictions)
372 amb 569 {
373 amb 622 if(key=="oneway" || key=="turns")
374 amb 577 document.forms["form"].elements["restrict-" + key].checked=routino.profile_restrictions[key][routino.transport];
375 amb 569 else
376 amb 577 document.forms["form"].elements["restrict-" + key].value=routino.profile_restrictions[key][routino.transport];
377 amb 569 }
378    
379     paramschanged=true;
380 amb 1486
381     updateURLs();
382 amb 569 }
383    
384    
385     //
386     // Change of highway in the form
387     //
388    
389 amb 1035 function formSetHighway(type,value) // called from router.html (with one argument)
390 amb 569 {
391 amb 1527 if(value == "+")
392 amb 985 {
393 amb 1515 value=routino.profile_highway[type][routino.transport];
394     value=10*Math.floor(value/10)+10;
395 amb 985 }
396 amb 1527 else if(value == "-")
397 amb 1515 {
398     value=routino.profile_highway[type][routino.transport]-10;
399     value=10*Math.ceil(value/10)-10;
400     }
401 amb 1527 else if(value == "=")
402 amb 1515 value=document.forms["form"].elements["highway-" + type].value;
403 amb 569
404 amb 1515 value=Number(value);
405     if(isNaN(value)) value= 50;
406     if(value>100) value=100;
407     if(value< 0) value= 0;
408    
409     document.forms["form"].elements["highway-" + type].value=value;
410     routino.profile_highway[type][routino.transport]=value;
411    
412 amb 569 paramschanged=true;
413 amb 1486
414     updateURLs();
415 amb 569 }
416    
417    
418     //
419     // Change of Speed in the form
420     //
421    
422 amb 1035 function formSetSpeed(type,value) // called from router.html (with one argument)
423 amb 569 {
424 amb 1527 if(value == "+")
425 amb 985 {
426 amb 1515 value=routino.profile_speed[type][routino.transport];
427     if(value<10) value=2*Math.floor(value/2)+2;
428     else if(value<30) value=5*Math.floor(value/5)+5;
429     else value=10*Math.floor(value/10)+10;
430 amb 985 }
431 amb 1527 else if(value == "-")
432 amb 1515 {
433     value=routino.profile_speed[type][routino.transport];
434     if(value<=10) value=2*Math.ceil(value/2)-2;
435     else if(value<=30) value=5*Math.ceil(value/5)-5;
436     else value=10*Math.ceil(value/10)-10;
437     }
438 amb 1527 else if(value == "=")
439 amb 1515 value=document.forms["form"].elements["speed-" + type].value;
440 amb 569
441 amb 1515 value=Number(value);
442     if(isNaN(value)) value= 60;
443     if(value>150) value=150;
444     if(value< 0) value= 0;
445    
446     document.forms["form"].elements["speed-" + type].value=value;
447     routino.profile_speed[type][routino.transport]=value;
448    
449 amb 569 paramschanged=true;
450 amb 1486
451     updateURLs();
452 amb 569 }
453    
454    
455     //
456 amb 574 // Change of Property in the form
457     //
458    
459 amb 1035 function formSetProperty(type,value) // called from router.html (with one argument)
460 amb 574 {
461 amb 1527 if(value == "+")
462 amb 985 {
463 amb 1515 value=routino.profile_property[type][routino.transport];
464     if(value>=40 && value<60) value=2*Math.floor(value/2)+2;
465     else value=5*Math.floor(value/5)+5;
466 amb 985 }
467 amb 1527 else if(value == "-")
468 amb 1515 {
469     value=routino.profile_property[type][routino.transport];
470     if(value>40 && value<=60) value=2*Math.ceil(value/2)-2;
471     else value=5*Math.ceil(value/5)-5;
472     }
473 amb 1527 else if(value == "=")
474 amb 1515 value=document.forms["form"].elements["property-" + type].value;
475 amb 574
476 amb 1515 value=Number(value);
477     if(isNaN(value)) value= 50;
478     if(value>100) value=100;
479     if(value< 0) value= 0;
480    
481     document.forms["form"].elements["property-" + type].value=value;
482     routino.profile_property[type][routino.transport]=value;
483    
484 amb 574 paramschanged=true;
485 amb 1486
486     updateURLs();
487 amb 574 }
488    
489    
490     //
491 amb 622 // Change of Restriction rule in the form
492 amb 569 //
493    
494 amb 1035 function formSetRestriction(type,value) // called from router.html (with one argument)
495 amb 569 {
496 amb 1515 if(type=="oneway" || type=="turns")
497 amb 985 {
498 amb 1515 if(value === undefined)
499 amb 1812 value=document.forms["form"].elements["restrict-" + type].checked;
500 amb 1515
501 amb 1812 document.forms["form"].elements["restrict-" + type].checked=value;
502 amb 1515 routino.profile_restrictions[type][routino.transport]=value;
503 amb 985 }
504 amb 1515 else if(type=="weight")
505 amb 985 {
506 amb 1527 if(value == "+")
507 amb 1515 value=routino.profile_restrictions[type][routino.transport]+5;
508 amb 1527 else if(value == "-")
509 amb 1515 value=routino.profile_restrictions[type][routino.transport]-5;
510 amb 1527 else if(value == "=")
511 amb 1515 value=document.forms["form"].elements["restrict-" + type].value;
512 amb 569
513 amb 1515 value=Number(value);
514     if(isNaN(value)) value= 0;
515     if(value>50) value=50;
516     if(value< 0) value= 0;
517    
518     document.forms["form"].elements["restrict-" + type].value=value;
519 amb 985 routino.profile_restrictions[type][routino.transport]=value;
520     }
521 amb 1515 else /* if(type=="height" || type=="width" || type=="length") */
522     {
523 amb 1527 if(value == "+")
524 amb 1515 value=routino.profile_restrictions[type][routino.transport]+1;
525 amb 1527 else if(value == "-")
526 amb 1515 value=routino.profile_restrictions[type][routino.transport]-1;
527 amb 1527 else if(value == "=")
528 amb 1515 value=document.forms["form"].elements["restrict-" + type].value;
529 amb 985
530 amb 1515 value=Number(value);
531     if(isNaN(value)) value= 0;
532     if(value>25) value=25;
533     if(value< 0) value= 0;
534    
535     document.forms["form"].elements["restrict-" + type].value=value;
536     routino.profile_restrictions[type][routino.transport]=value;
537     }
538    
539 amb 569 paramschanged=true;
540 amb 1486
541     updateURLs();
542 amb 569 }
543    
544    
545     //
546 amb 577 // Set the feature coordinates from the form when the form changes.
547     //
548    
549 amb 1035 function formSetCoords(marker,lon,lat) // called from router.html (with one argument)
550 amb 577 {
551 amb 1005 clearSearchResult(marker);
552    
553 amb 1493 if(lon === undefined && lat === undefined)
554 amb 985 {
555 amb 1034 lon=document.forms["form"].elements["lon" + marker].value;
556     lat=document.forms["form"].elements["lat" + marker].value;
557 amb 985 }
558 amb 1034
559 amb 1493 if(lon === "" && lat === "")
560 amb 985 {
561 amb 1034 document.forms["form"].elements["lon" + marker].value="";
562     document.forms["form"].elements["lat" + marker].value="";
563 amb 985
564 amb 1034 routino.point[marker].lon="";
565 amb 1483 routino.point[marker].lat="";
566 amb 1486
567     updateURLs();
568 amb 985 }
569 amb 1034 else
570     {
571 amb 1483 var lonlat;
572    
573 amb 1493 if(lon==="")
574 amb 1034 {
575 amb 1483 lonlat=map.getCenter().clone();
576 amb 1034 lonlat.transform(epsg900913,epsg4326);
577 amb 985
578 amb 1034 lon=lonlat.lon;
579     }
580 amb 577
581 amb 1034 if(lon<-180) lon=-180;
582     if(lon>+180) lon=+180;
583 amb 577
584 amb 1493 if(lat==="")
585 amb 1034 {
586 amb 1483 lonlat=map.getCenter().clone();
587 amb 1034 lonlat.transform(epsg900913,epsg4326);
588 amb 577
589 amb 1034 lat=lonlat.lat;
590     }
591 amb 577
592 amb 1034 if(lat<-90 ) lat=-90 ;
593     if(lat>+90 ) lat=+90 ;
594 amb 577
595 amb 1483 lonlat = new OpenLayers.LonLat(lon,lat);
596 amb 1034 lonlat.transform(epsg4326,epsg900913);
597    
598     markers[marker].move(lonlat);
599    
600     markersmoved=true;
601    
602     document.forms["form"].elements["lon" + marker].value=format5f(lon);
603     document.forms["form"].elements["lat" + marker].value=format5f(lat);
604    
605     routino.point[marker].lon=lon;
606     routino.point[marker].lat=lat;
607     routino.point[marker].used=true;
608 amb 1039
609     markerCheckHome(marker);
610 amb 1034 }
611 amb 577 }
612    
613    
614     //
615 amb 1032 // Set the search field from the form when the form changes.
616 amb 1001 //
617    
618 amb 1035 function formSetSearch(marker,search) // called from event handler linked to router.html (with one argument)
619 amb 1001 {
620 amb 1005 clearSearchResult(marker);
621    
622 amb 1493 if(search === undefined)
623 amb 1001 {
624     routino.point[marker].search=document.forms["form"].elements["search" + marker].value;
625    
626     DoSearch(marker);
627     }
628     else
629     {
630     document.forms["form"].elements["search" + marker].value=search;
631    
632     routino.point[marker].search=search;
633     }
634     }
635    
636    
637     //
638 amb 1813 // Change of loop or reverse option in the form
639     //
640    
641     function formSetLoopReverse(type,value) // called from router.html (with one argument)
642     {
643     if(value === undefined)
644     value=document.forms["form"].elements[type].checked;
645    
646     document.forms["form"].elements[type].checked=value;
647    
648     if(type == "loop")
649     routino.loop=value;
650     else
651     routino.reverse=value;
652    
653     updateURLs();
654     }
655    
656    
657     //
658 amb 577 // Format a number in printf("%.5f") format.
659     //
660    
661     function format5f(number)
662     {
663     var newnumber=Math.floor(number*100000+0.5);
664     var delta=0;
665    
666     if(newnumber>=0 && newnumber<100000) delta= 100000;
667     if(newnumber<0 && newnumber>-100000) delta=-100000;
668    
669     var string=String(newnumber+delta);
670    
671     var intpart =string.substring(0,string.length-5);
672     var fracpart=string.substring(string.length-5,string.length);
673    
674     if(delta>0) intpart="0";
675     if(delta<0) intpart="-0";
676    
677     return(intpart + "." + fracpart);
678     }
679    
680    
681     //
682     // Build a set of URL arguments
683     //
684    
685 amb 986 function buildURLArguments(lang)
686 amb 577 {
687 amb 986 var url= "transport=" + routino.transport;
688 amb 577
689     for(var marker=1;marker<=vismarkers;marker++)
690 amb 986 if(routino.point[marker].active)
691 amb 577 {
692 amb 1763 url=url + ";lon" + marker + "=" + format5f(routino.point[marker].lon);
693     url=url + ";lat" + marker + "=" + format5f(routino.point[marker].lat);
694 amb 1493 if(routino.point[marker].search !== "")
695 amb 1001 url=url + ";search" + marker + "=" + encodeURIComponent(routino.point[marker].search);
696 amb 577 }
697    
698     for(var key in routino.profile_highway)
699     if(routino.profile_highway[key][routino.transport]!=routino_default.profile_highway[key][routino.transport])
700     url=url + ";highway-" + key + "=" + routino.profile_highway[key][routino.transport];
701    
702     for(var key in routino.profile_speed)
703     if(routino.profile_speed[key][routino.transport]!=routino_default.profile_speed[key][routino.transport])
704     url=url + ";speed-" + key + "=" + routino.profile_speed[key][routino.transport];
705    
706     for(var key in routino.profile_property)
707     if(routino.profile_property[key][routino.transport]!=routino_default.profile_property[key][routino.transport])
708     url=url + ";property-" + key + "=" + routino.profile_property[key][routino.transport];
709    
710     for(var key in routino.restrictions)
711     if(routino.profile_restrictions[key][routino.transport]!=routino_default.profile_restrictions[key][routino.transport])
712     url=url + ";" + key + "=" + routino.profile_restrictions[key][routino.transport];
713    
714 amb 1813 if(routino.loop)
715     url=url + ";loop=true";
716    
717     if(routino.reverse)
718     url=url + ";reverse=true";
719    
720 amb 986 if(lang && routino.language)
721 amb 577 url=url + ";language=" + routino.language;
722    
723     return(url);
724     }
725    
726    
727     //
728 amb 986 // Build a set of URL arguments for the map location
729 amb 569 //
730    
731 amb 986 function buildMapArguments()
732 amb 569 {
733 amb 1009 var lonlat = map.getCenter().clone();
734     lonlat.transform(epsg900913,epsg4326);
735 amb 569
736 amb 986 var zoom = map.getZoom() + map.minZoomLevel;
737    
738     return "lat=" + format5f(lonlat.lat) + ";lon=" + format5f(lonlat.lon) + ";zoom=" + zoom;
739 amb 569 }
740    
741    
742     //
743 amb 1486 // Update the URLs
744 amb 986 //
745    
746 amb 1486 function updateURLs()
747 amb 986 {
748 amb 1486 var urlargs1=buildURLArguments(true);
749     var urlargs2=buildURLArguments(false);
750     var mapargs=buildMapArguments();
751 amb 986
752 amb 1486 var links=document.getElementsByTagName("a");
753 amb 986
754 amb 1486 for(var i=0; i<links.length; i++)
755     {
756     var element=links[i];
757 amb 986
758 amb 1486 if(element.id == "permalink_url")
759     element.href=location.pathname + "?" + urlargs1 + ";" + mapargs;
760    
761     if(element.id == "visualiser_url")
762     element.href="visualiser.html" + "?" + mapargs;
763    
764     if(element.id == "edit_url")
765     element.href=mapprops.editurl + "?" + mapargs;
766    
767     if(element.id.match(/^lang_([a-zA-Z-]+)_url$/))
768     element.href="router.html" + "." + RegExp.$1 + "?" + urlargs2 + ";" + mapargs;
769     }
770 amb 986 }
771    
772    
773 amb 577 ////////////////////////////////////////////////////////////////////////////////
774     ///////////////////////////////// Map handling /////////////////////////////////
775     ////////////////////////////////////////////////////////////////////////////////
776 amb 569
777     var map;
778 amb 933 var layerMap=[], layerVectors, layerGPX;
779 amb 569 var epsg4326, epsg900913;
780 amb 1491 var routing_type;
781 amb 569
782 amb 985 //
783 amb 569 // Initialise the 'map' object
784     //
785    
786 amb 1035 function map_init() // called from router.html
787 amb 569 {
788 amb 1491 // Create the map (Map URLs and limits are in mapprops.js)
789 amb 985
790 amb 569 epsg4326=new OpenLayers.Projection("EPSG:4326");
791     epsg900913=new OpenLayers.Projection("EPSG:900913");
792    
793     map = new OpenLayers.Map ("map",
794     {
795     controls:[
796     new OpenLayers.Control.Navigation(),
797     new OpenLayers.Control.PanZoomBar(),
798     new OpenLayers.Control.ScaleLine(),
799     new OpenLayers.Control.LayerSwitcher()
800     ],
801    
802     projection: epsg900913,
803     displayProjection: epsg4326,
804    
805 amb 933 minZoomLevel: mapprops.zoomout,
806     numZoomLevels: mapprops.zoomin-mapprops.zoomout+1,
807 amb 1018 maxResolution: 156543.03390625 / Math.pow(2,mapprops.zoomout),
808 amb 569
809 amb 1013 restrictedExtent: new OpenLayers.Bounds(mapprops.westedge,mapprops.southedge,mapprops.eastedge,mapprops.northedge).transform(epsg4326,epsg900913)
810 amb 569 });
811    
812 amb 1491 // Get a URL for the tile (mostly copied from OpenLayers/Layer/XYZ.js).
813    
814     function limitedUrl(bounds)
815     {
816     var res = this.map.getResolution();
817    
818     var x = Math.round((bounds.left - this.maxExtent.left) / (res * this.tileSize.w));
819     var y = Math.round((this.maxExtent.top - bounds.top) / (res * this.tileSize.h));
820     var z = this.map.getZoom() + this.map.minZoomLevel;
821    
822     var limit = Math.pow(2, z);
823     x = ((x % limit) + limit) % limit;
824    
825     var xyz = {"x": x, "y": y, "z": z};
826     var url = this.url;
827    
828     if (OpenLayers.Util.isArray(url))
829     {
830     var s = "" + xyz.x + xyz.y + xyz.z;
831     url = this.selectUrl(s, url);
832     }
833    
834     return OpenLayers.String.format(url, xyz);
835     }
836    
837 amb 933 // Add map tile layers
838 amb 569
839 amb 1491 for(var l=0; l<mapprops.mapdata.length; l++)
840 amb 933 {
841 amb 1491 var urls;
842    
843     if(OpenLayers.Util.isArray(mapprops.mapdata[l].tiles.subdomains))
844     {
845     urls=[];
846    
847     for(var s=0; s<mapprops.mapdata[l].tiles.subdomains.length; s++)
848     urls.push(mapprops.mapdata[l].tiles.url.replace(/\${s}/,mapprops.mapdata[l].tiles.subdomains[s]));
849     }
850     else
851     urls=mapprops.mapdata[l].tiles.url;
852    
853 amb 933 layerMap[l] = new OpenLayers.Layer.TMS(mapprops.mapdata[l].label,
854 amb 1491 urls,
855 amb 933 {
856     getURL: limitedUrl,
857     displayOutsideMaxExtent: true,
858     buffer: 1
859     });
860     map.addLayer(layerMap[l]);
861     }
862 amb 569
863 amb 1239 // Update the attribution if the layer changes
864 amb 569
865 amb 1239 function change_attribution_event(event)
866 amb 569 {
867 amb 1491 for(var l=0; l<mapprops.mapdata.length; l++)
868     if(layerMap[l] == event.layer)
869 amb 1239 change_attribution(l);
870     }
871 amb 569
872 amb 1491 map.events.register("changelayer",layerMap,change_attribution_event);
873    
874 amb 1239 function change_attribution(l)
875     {
876     var data_url =mapprops.mapdata[l].attribution.data_url;
877     var data_text=mapprops.mapdata[l].attribution.data_text;
878     var tile_url =mapprops.mapdata[l].attribution.tile_url;
879     var tile_text=mapprops.mapdata[l].attribution.tile_text;
880 amb 569
881 amb 1239 document.getElementById("attribution_data").innerHTML="<a href=\"" + data_url + "\" target=\"data_attribution\">" + data_text + "</a>";
882     document.getElementById("attribution_tile").innerHTML="<a href=\"" + tile_url + "\" target=\"tile_attribution\">" + tile_text + "</a>";
883     }
884    
885     change_attribution(0);
886    
887 amb 569 // Define a GPX layer but don't add it yet
888    
889     layerGPX={shortest: null, quickest: null};
890    
891     gpx_style={shortest: new OpenLayers.Style({},{strokeWidth: 3, strokeColor: "#00FF00"}),
892     quickest: new OpenLayers.Style({},{strokeWidth: 3, strokeColor: "#0000FF"})};
893 amb 985
894 amb 569 // Add a vectors layer
895 amb 985
896 amb 1328 layerVectors = new OpenLayers.Layer.Vector("Markers",{displayInLayerSwitcher: false});
897 amb 569 map.addLayer(layerVectors);
898    
899     // A set of markers
900    
901 amb 574 markers={};
902 amb 569 markersmoved=false;
903     paramschanged=false;
904    
905 amb 1012 for(var marker=1;marker<=mapprops.maxmarkers;marker++)
906 amb 569 {
907 amb 987 markers[marker] = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(0,0),{},
908 amb 1483 new OpenLayers.Style({},{externalGraphic: "icons/marker-" + marker + "-red.png",
909 amb 987 fillColor: "white",
910     graphicYOffset: -25,
911     graphicWidth: 21,
912     graphicHeight: 25,
913     display: "none"}));
914 amb 569
915 amb 987 layerVectors.addFeatures([markers[marker]]);
916 amb 569 }
917    
918     // A function to drag the markers
919    
920     var drag = new OpenLayers.Control.DragFeature(layerVectors,
921 amb 1656 {onDrag: dragMarkerMove,
922     onComplete: dragMarkerComplete });
923 amb 569 map.addControl(drag);
924     drag.activate();
925    
926     // Markers to highlight a selected point
927    
928 amb 577 for(var highlight in highlights)
929 amb 569 {
930     highlights[highlight] = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(0,0),{},
931 amb 577 new OpenLayers.Style({},{strokeColor: route_dark_colours[highlight],
932 amb 574 fillColor: "white",
933 amb 569 pointRadius: 10,
934     strokeWidth: 4,
935     fillOpacity: 0,
936     display: "none"}));
937    
938     layerVectors.addFeatures([highlights[highlight]]);
939     }
940    
941 amb 574 // A popup for routing results
942    
943 amb 577 for(var popup in popups)
944     popups[popup] = createPopup(popup);
945 amb 574
946 amb 569 // Move the map
947    
948 amb 1486 map.events.register("moveend", map, updateURLs);
949    
950 amb 1491 var lon =args["lon"];
951     var lat =args["lat"];
952     var zoom=args["zoom"];
953    
954 amb 1493 if(lon !== undefined && lat !== undefined && zoom !== undefined)
955 amb 569 {
956 amb 933 if(lon<mapprops.westedge) lon=mapprops.westedge;
957     if(lon>mapprops.eastedge) lon=mapprops.eastedge;
958    
959     if(lat<mapprops.southedge) lat=mapprops.southedge;
960     if(lat>mapprops.northedge) lat=mapprops.northedge;
961    
962     if(zoom<mapprops.zoomout) zoom=mapprops.zoomout;
963     if(zoom>mapprops.zoomin) zoom=mapprops.zoomin;
964    
965 amb 1009 var lonlat = new OpenLayers.LonLat(lon,lat);
966     lonlat.transform(epsg4326,epsg900913);
967 amb 569
968     map.moveTo(lonlat,zoom-map.minZoomLevel);
969     }
970 amb 1491 else
971     {
972     map.setCenter(map.restrictedExtent.getCenterLonLat(), map.getZoomForExtent(map.restrictedExtent,true));
973     map.maxResolution = map.getResolution();
974     }
975 amb 1334
976     // Unhide editing URL if variable set
977    
978 amb 1493 if(mapprops.editurl !== undefined && mapprops.editurl !== "")
979 amb 1334 {
980 amb 1483 var edit_url=document.getElementById("edit_url");
981 amb 1334
982     edit_url.style.display="";
983     edit_url.href=mapprops.editurl;
984     }
985 amb 1486
986     updateURLs();
987 amb 569 }
988    
989    
990     //
991 amb 1656 // Callback for a marker drag occuring on the map.
992 amb 569 //
993    
994 amb 1656 function dragMarkerMove(feature,pixel)
995 amb 569 {
996 amb 574 for(var marker in markers)
997 amb 569 if(feature==markers[marker])
998 amb 1657 dragMarkerSetForm(marker);
999 amb 569 }
1000    
1001    
1002     //
1003 amb 1656 // Callback for completing a marker drag on the map.
1004 amb 569 //
1005    
1006 amb 1656 function dragMarkerComplete(feature,pixel)
1007 amb 569 {
1008 amb 574 for(var marker in markers)
1009 amb 569 if(feature==markers[marker])
1010 amb 1657 dragMarkerSetForm(marker);
1011 amb 1486
1012     updateURLs();
1013 amb 569 }
1014    
1015    
1016 amb 985 //
1017 amb 1656 // Set the feature coordinates in the form after dragging it on the map.
1018 amb 985 //
1019    
1020 amb 1656 function dragMarkerSetForm(marker)
1021 amb 985 {
1022     var lonlat = new OpenLayers.LonLat(markers[marker].geometry.x, markers[marker].geometry.y);
1023 amb 1009 lonlat.transform(epsg900913,epsg4326);
1024 amb 985
1025 amb 1656 formSetCoords(marker,lonlat.lon,lonlat.lat);
1026     }
1027 amb 985
1028 amb 1656
1029     ////////////////////////////////////////////////////////////////////////////////
1030     /////////////////////////////// Marker dragging ////////////////////////////////
1031     ////////////////////////////////////////////////////////////////////////////////
1032    
1033     var dragged_waypoint=null,dragged_marker=null;
1034     var dragged_waypoint_over=null,dragged_marker_over=null;
1035     var dragged_icon_x,dragged_icon_y;
1036    
1037     //
1038     // Drag a waypoint up or down the list.
1039     //
1040    
1041     function dragWaypointStart(e)
1042     {
1043     var w=e.target;
1044    
1045     while(w!=null && w.className != "waypoint")
1046     w=w.parentElement;
1047    
1048     if(w===null)
1049     return;
1050    
1051     w.style.opacity = "0.5";
1052    
1053     dragged_waypoint=w;
1054     dragged_marker=Number.parseInt(dragged_waypoint.id.substring(8));
1055    
1056     dragged_icon_x=e.clientX-e.target.offsetLeft;
1057     dragged_icon_y=e.clientY-e.target.offsetTop;
1058 amb 985 }
1059    
1060 amb 1656 function dragWaypointEnd(e)
1061     {
1062     e.preventDefault();
1063 amb 985
1064 amb 1656 if(dragged_waypoint===null)
1065     return;
1066    
1067     dragged_waypoint.style.opacity = "";
1068    
1069     dragged_waypoint=null;
1070     dragged_marker=null;
1071    
1072     if(dragged_waypoint_over===null)
1073     return;
1074    
1075     dragged_waypoint_over.style.border = "";
1076    
1077     dragged_waypoint_over=null;
1078     dragged_marker_over=null;
1079     }
1080    
1081    
1082     //
1083     // Drag a waypoint over another one up or down the list.
1084     //
1085    
1086     function dragWaypointEnter(e)
1087     {
1088     var w=e.target;
1089    
1090     while(w!=null && w.className != "waypoint")
1091     w=w.parentElement;
1092    
1093     if(w===null)
1094     return;
1095    
1096     if(dragged_waypoint_over!==null)
1097     dragged_waypoint_over.style.border = "";
1098    
1099     if(w==dragged_waypoint)
1100     return;
1101    
1102     dragged_waypoint_over=w;
1103     dragged_marker_over=Number.parseInt(dragged_waypoint_over.id.substring(8));
1104    
1105     if(dragged_marker>dragged_marker_over)
1106     w.style.borderTop = "3px solid black";
1107     else
1108     w.style.borderBottom = "3px solid black";
1109     }
1110    
1111     function dragWaypointOver(e)
1112     {
1113     e.preventDefault();
1114     }
1115    
1116     function dragWaypointLeave(e)
1117     {
1118     var w=e.target;
1119    
1120     while(w!=null && w.className != "waypoint")
1121     w=w.parentElement;
1122    
1123     if(w===null)
1124     return;
1125    
1126     if(w==dragged_waypoint_over)
1127     return;
1128    
1129     w.style.border = "";
1130     }
1131    
1132    
1133     //
1134     // Drop the waypoint after dragging up or down the list.
1135     //
1136    
1137     function dragWaypointDrop(e)
1138     {
1139     e.preventDefault();
1140    
1141     if(dragged_marker_over===null)
1142     return;
1143    
1144     if(dragged_marker_over>dragged_marker)
1145     for(var m=dragged_marker;m<dragged_marker_over;m++)
1146     markerSwap(m,m+1);
1147    
1148     if(dragged_marker_over<dragged_marker)
1149     for(var m=dragged_marker;m>dragged_marker_over;m--)
1150     markerSwap(m,m-1);
1151     }
1152    
1153    
1154     //
1155     // Drag a waypoint over the map.
1156     //
1157    
1158     function dragWaypointMapEnter(e)
1159     {
1160     e.preventDefault();
1161    
1162     if(dragged_waypoint_over!==null)
1163     dragged_waypoint_over.style.border = "";
1164     }
1165    
1166     function dragWaypointMapOver(e)
1167     {
1168     e.preventDefault();
1169     }
1170    
1171     function dragWaypointMapLeave(e)
1172     {
1173     e.preventDefault();
1174     }
1175    
1176    
1177     //
1178     // Drop the waypoint after dragging it over the map.
1179     //
1180    
1181     function dragWaypointMapDrop(e)
1182     {
1183     e.preventDefault();
1184    
1185     var rect = document.getElementById("map").getBoundingClientRect();
1186    
1187 amb 1793 var lonlat = map.getLonLatFromViewPortPx(new OpenLayers.Pixel(e.clientX-rect.left-window.scrollX-dragged_icon_x+8,
1188     e.clientY-rect.top -window.scrollY-dragged_icon_y+21));
1189 amb 1656 lonlat.transform(epsg900913,epsg4326);
1190    
1191     formSetCoords(dragged_marker,lonlat.lon,lonlat.lat);
1192 amb 1657
1193     if(!routino.point[dragged_marker].active)
1194     markerToggleMap(dragged_marker);
1195 amb 1656 }
1196    
1197    
1198 amb 577 ////////////////////////////////////////////////////////////////////////////////
1199     /////////////////////////////// Marker handling ////////////////////////////////
1200     ////////////////////////////////////////////////////////////////////////////////
1201    
1202 amb 569 //
1203 amb 574 // Toggle a marker on the map.
1204 amb 569 //
1205    
1206 amb 1035 function markerToggleMap(marker) // called from router.html
1207 amb 569 {
1208 amb 1034 if(!routino.point[marker].used)
1209     {
1210     routino.point[marker].used=true;
1211     markerCentre(marker);
1212     markerCoords(marker);
1213     }
1214 amb 1005
1215 amb 1032 markerAddRemoveMap(marker,!routino.point[marker].active);
1216     }
1217    
1218    
1219     //
1220     // Show or hide a marker on the map.
1221     //
1222    
1223     function markerAddRemoveMap(marker,active)
1224     {
1225     if(active)
1226     markerAddMap(marker);
1227     else
1228 amb 574 markerRemoveMap(marker);
1229     }
1230    
1231    
1232     //
1233     // Show a marker on the map.
1234     //
1235    
1236     function markerAddMap(marker)
1237     {
1238 amb 1005 clearSearchResult(marker);
1239    
1240 amb 577 markers[marker].style.display = "";
1241 amb 985 routino.point[marker].active=true;
1242 amb 1034 routino.point[marker].used=true;
1243 amb 574
1244 amb 577 updateIcon(marker);
1245 amb 574
1246     markersmoved=true;
1247 amb 1486
1248     updateURLs();
1249 amb 1948
1250     updateSearchButtons();
1251 amb 574 }
1252    
1253    
1254     //
1255     // Remove a marker from the map.
1256     //
1257    
1258     function markerRemoveMap(marker)
1259     {
1260 amb 1005 clearSearchResult(marker);
1261    
1262 amb 577 markers[marker].style.display = "none";
1263 amb 985 routino.point[marker].active=false;
1264 amb 574
1265 amb 577 updateIcon(marker);
1266 amb 574
1267 amb 577 markersmoved=true;
1268 amb 1486
1269     updateURLs();
1270 amb 1948
1271     updateSearchButtons();
1272 amb 577 }
1273 amb 574
1274 amb 577
1275     //
1276 amb 1001 // Display search string for the marker
1277     //
1278    
1279 amb 1035 function markerSearch(marker) // called from router.html
1280 amb 1001 {
1281 amb 1005 clearSearchResult(marker);
1282    
1283 amb 1011 document.getElementById("coords" + marker).style.display="none";
1284     document.getElementById("search" + marker).style.display="";
1285 amb 1001 }
1286    
1287    
1288     //
1289     // Display coordinates for the marker
1290     //
1291    
1292 amb 1035 function markerCoords(marker) // called from router.html
1293 amb 1001 {
1294 amb 1005 clearSearchResult(marker);
1295    
1296 amb 1011 document.getElementById("search" + marker).style.display="none";
1297     document.getElementById("coords" + marker).style.display="";
1298 amb 1001 }
1299    
1300    
1301     //
1302 amb 577 // Centre the marker on the map
1303     //
1304    
1305 amb 1035 function markerCentre(marker) // called from router.html
1306 amb 577 {
1307 amb 1034 if(!routino.point[marker].used)
1308     return;
1309    
1310 amb 1005 clearSearchResult(marker);
1311    
1312 amb 985 var lonlat=map.getCenter().clone();
1313 amb 1009 lonlat.transform(epsg900913,epsg4326);
1314 amb 577
1315 amb 1656 formSetCoords(marker,lonlat.lon,lonlat.lat);
1316 amb 574 }
1317    
1318    
1319     //
1320 amb 984 // Centre the map on the marker
1321     //
1322    
1323 amb 1035 function markerRecentre(marker) // called from router.html
1324 amb 984 {
1325 amb 1034 if(!routino.point[marker].used)
1326     return;
1327    
1328 amb 1005 clearSearchResult(marker);
1329    
1330 amb 1483 var lon=routino.point[marker].lon;
1331     var lat=routino.point[marker].lat;
1332 amb 984
1333 amb 1009 var lonlat = new OpenLayers.LonLat(lon,lat);
1334     lonlat.transform(epsg4326,epsg900913);
1335 amb 984
1336     map.panTo(lonlat);
1337     }
1338    
1339    
1340     //
1341 amb 574 // Clear the current marker.
1342     //
1343    
1344 amb 1035 function markerRemove(marker) // called from router.html
1345 amb 574 {
1346 amb 1005 clearSearchResult(marker);
1347    
1348 amb 1033 for(var m=marker;m<vismarkers;m++)
1349 amb 1039 markerCopy(m,m+1);
1350 amb 574
1351 amb 1033 markerRemoveForm(vismarkers--);
1352 amb 574
1353     if(vismarkers==1)
1354     markerAddAfter(1);
1355 amb 1546
1356     updateSearchButtons();
1357 amb 574 }
1358    
1359    
1360     //
1361     // Add a marker before the current one.
1362     //
1363    
1364     function markerAddBefore(marker)
1365     {
1366 amb 1012 if(vismarkers==mapprops.maxmarkers || marker==1)
1367 amb 574 return false;
1368    
1369 amb 1546 clearSearchResult(marker);
1370    
1371 amb 1033 markerAddForm(++vismarkers);
1372 amb 574
1373 amb 1033 for(var m=vismarkers;m>marker;m--)
1374 amb 1039 markerCopy(m,m-1);
1375 amb 574
1376 amb 1033 markerClearForm(marker-1);
1377 amb 574 }
1378    
1379    
1380     //
1381     // Add a marker after the current one.
1382     //
1383    
1384 amb 1035 function markerAddAfter(marker) // called from router.html
1385 amb 574 {
1386 amb 1012 if(vismarkers==mapprops.maxmarkers)
1387 amb 574 return false;
1388    
1389 amb 1546 clearSearchResult(marker);
1390    
1391 amb 1033 markerAddForm(++vismarkers);
1392 amb 574
1393 amb 1033 for(var m=vismarkers;m>(marker+1);m--)
1394 amb 1039 markerCopy(m,m-1);
1395 amb 574
1396 amb 1033 markerClearForm(marker+1);
1397 amb 569 }
1398    
1399    
1400     //
1401 amb 577 // Set this marker as the home location.
1402     //
1403    
1404 amb 1035 function markerHome(marker) // called from router.html
1405 amb 577 {
1406 amb 1034 if(!routino.point[marker].used)
1407 amb 1036 {
1408     markerMoveHome(marker);
1409     }
1410 amb 1039 else
1411     {
1412     clearSearchResult(marker);
1413 amb 1034
1414 amb 1039 markerSetClearHome(marker,!routino.point[marker].home);
1415     }
1416 amb 577 }
1417    
1418    
1419     //
1420 amb 984 // Set this marker as the current location.
1421     //
1422    
1423 amb 1035 function markerLocate(marker) // called from router.html
1424 amb 984 {
1425 amb 1005 clearSearchResult(marker);
1426    
1427 amb 1830 function success(position)
1428     {
1429     formSetCoords(marker,position.coords.longitude,position.coords.latitude);
1430     markerAddMap(marker);
1431     }
1432    
1433     function failure(error)
1434     {
1435     alert("Error: " + error.message);
1436     }
1437    
1438 amb 984 if(navigator.geolocation)
1439 amb 1830 navigator.geolocation.getCurrentPosition(success,failure);
1440     else
1441     alert("Error: Geolocation unavailable");
1442 amb 984 }
1443    
1444    
1445     //
1446 amb 1546 // Update the search buttons enable/disable.
1447     //
1448    
1449     function updateSearchButtons()
1450     {
1451     var markersactive=0;
1452    
1453     for(var m=1;m<=vismarkers;m++)
1454     if(routino.point[m].active)
1455     markersactive++;
1456    
1457     if(markersactive<2)
1458     {
1459 amb 1877 document.getElementById("shortest1").disabled="disabled";
1460     document.getElementById("quickest1").disabled="disabled";
1461     document.getElementById("shortest2").disabled="disabled";
1462     document.getElementById("quickest2").disabled="disabled";
1463 amb 1546 }
1464     else
1465     {
1466 amb 1877 document.getElementById("shortest1").disabled="";
1467     document.getElementById("quickest1").disabled="";
1468     document.getElementById("shortest2").disabled="";
1469     document.getElementById("quickest2").disabled="";
1470 amb 1546 }
1471     }
1472    
1473    
1474     //
1475 amb 577 // Update an icon to set colours and home or normal marker.
1476     //
1477    
1478     function updateIcon(marker)
1479     {
1480 amb 1036 if(routino.point[marker].home)
1481 amb 577 {
1482 amb 985 if(routino.point[marker].active)
1483 amb 1475 document.getElementById("icon" + marker).src="icons/marker-home-red.png";
1484 amb 577 else
1485 amb 1475 document.getElementById("icon" + marker).src="icons/marker-home-grey.png";
1486 amb 577
1487     markers[marker].style.externalGraphic="icons/marker-home-red.png";
1488     }
1489     else
1490     {
1491 amb 985 if(routino.point[marker].active)
1492 amb 1475 document.getElementById("icon" + marker).src="icons/marker-" + marker + "-red.png";
1493 amb 577 else
1494 amb 1475 document.getElementById("icon" + marker).src="icons/marker-" + marker + "-grey.png";
1495 amb 577
1496     markers[marker].style.externalGraphic="icons/marker-" + marker + "-red.png";
1497     }
1498    
1499     layerVectors.drawFeature(markers[marker]);
1500     }
1501    
1502    
1503     //
1504 amb 1036 // Move the marker to the home location
1505 amb 577 //
1506    
1507 amb 1036 function markerMoveHome(marker)
1508 amb 577 {
1509 amb 1493 if(homelon===null || homelat===null)
1510 amb 1036 return;
1511 amb 577
1512 amb 1036 routino.point[marker].home=true;
1513     routino.point[marker].used=true;
1514 amb 577
1515 amb 1036 formSetCoords(marker,homelon,homelat);
1516     markerAddMap(marker);
1517     }
1518    
1519    
1520     //
1521     // Set or clear the home marker icon
1522     //
1523    
1524     function markerSetClearHome(marker,home)
1525     {
1526 amb 577 var cookie;
1527     var date = new Date();
1528    
1529 amb 1036 if(home)
1530 amb 577 {
1531 amb 1831 homelat=format5f(routino.point[marker].lat);
1532     homelon=format5f(routino.point[marker].lon);
1533 amb 577
1534 amb 1036 cookie="Routino-home=lon:" + homelon + ":lat:" + homelat;
1535    
1536 amb 577 date.setUTCFullYear(date.getUTCFullYear()+5);
1537    
1538 amb 1036 routino.point[marker].home=true;
1539 amb 577 }
1540     else
1541     {
1542 amb 1036 homelat=null;
1543     homelon=null;
1544    
1545 amb 577 cookie="Routino-home=unset";
1546    
1547     date.setUTCFullYear(date.getUTCFullYear()-1);
1548    
1549 amb 1036 routino.point[marker].home=false;
1550 amb 577 }
1551    
1552     document.cookie=cookie + ";expires=" + date.toGMTString();
1553    
1554 amb 1039 updateIcon(marker);
1555    
1556 amb 1483 for(var m=1;m<=mapprops.maxmarkers;m++)
1557 amb 1039 markerCheckHome(m);
1558     }
1559 amb 1036
1560 amb 1039
1561     //
1562     // Check if a marker is the home marker
1563     //
1564    
1565     function markerCheckHome(marker)
1566     {
1567     var home=routino.point[marker].home;
1568    
1569     if(routino.point[marker].lon==homelon && routino.point[marker].lat==homelat)
1570     routino.point[marker].home=true;
1571     else
1572     routino.point[marker].home=false;
1573    
1574     if(home!=routino.point[marker].home)
1575     updateIcon(marker);
1576 amb 577 }
1577    
1578    
1579     //
1580 amb 574 // Move this marker up.
1581     //
1582    
1583 amb 1035 function markerMoveUp(marker) // called from router.html
1584 amb 574 {
1585     if(marker==1)
1586 amb 984 {
1587     for(var m=1;m<vismarkers;m++)
1588     markerSwap(m,m+1);
1589     }
1590     else
1591     markerSwap(marker,marker-1);
1592 amb 574 }
1593    
1594    
1595     //
1596     // Move this marker down.
1597     //
1598    
1599 amb 1035 function markerMoveDown(marker) // called from router.html
1600 amb 574 {
1601     if(marker==vismarkers)
1602 amb 984 {
1603     for(var m=vismarkers;m>1;m--)
1604     markerSwap(m,m-1);
1605     }
1606     else
1607     markerSwap(marker,marker+1);
1608 amb 574 }
1609    
1610    
1611     //
1612 amb 1039 // Copy a marker from one place to another.
1613 amb 1033 //
1614    
1615 amb 1039 function markerCopy(marker1,marker2)
1616 amb 1033 {
1617     for(var element in routino.point[marker2])
1618     routino.point[marker1][element]=routino.point[marker2][element];
1619    
1620     document.getElementById("search" + marker1).style.display=document.getElementById("search" + marker2).style.display;
1621    
1622     document.getElementById("coords" + marker1).style.display=document.getElementById("coords" + marker2).style.display;
1623    
1624     document.forms["form"].elements["search" + marker1].value=document.forms["form"].elements["search" + marker2].value;
1625    
1626     formSetCoords(marker1,routino.point[marker1].lon,routino.point[marker1].lat);
1627    
1628     markerAddRemoveMap(marker1,routino.point[marker1].active);
1629     }
1630    
1631    
1632     //
1633 amb 574 // Swap a pair of markers.
1634     //
1635    
1636     function markerSwap(marker1,marker2)
1637     {
1638 amb 1033 for(var element in routino.point[marker2])
1639     {
1640     var temp=routino.point[marker1][element];
1641     routino.point[marker1][element]=routino.point[marker2][element];
1642     routino.point[marker2][element]=temp;
1643     }
1644 amb 574
1645 amb 1033 var search_display=document.getElementById("search" + marker1).style.display;
1646     document.getElementById("search" + marker1).style.display=document.getElementById("search" + marker2).style.display;
1647     document.getElementById("search" + marker2).style.display=search_display;
1648 amb 574
1649 amb 1033 var coords_display=document.getElementById("coords" + marker1).style.display;
1650     document.getElementById("coords" + marker1).style.display=document.getElementById("coords" + marker2).style.display;
1651     document.getElementById("coords" + marker2).style.display=coords_display;
1652    
1653     var search_value=document.forms["form"].elements["search" + marker1].value;
1654     document.forms["form"].elements["search" + marker1].value=document.forms["form"].elements["search" + marker2].value;
1655     document.forms["form"].elements["search" + marker2].value=search_value;
1656    
1657     formSetCoords(marker1,routino.point[marker1].lon,routino.point[marker1].lat);
1658     formSetCoords(marker2,routino.point[marker2].lon,routino.point[marker2].lat);
1659    
1660     markerAddRemoveMap(marker1,routino.point[marker1].active);
1661     markerAddRemoveMap(marker2,routino.point[marker2].active);
1662 amb 574 }
1663    
1664    
1665     //
1666     // Reverse the markers.
1667     //
1668    
1669 amb 1035 function markersReverse() // called from router.html
1670 amb 574 {
1671     for(var marker=1;marker<=vismarkers/2;marker++)
1672     markerSwap(marker,vismarkers+1-marker);
1673 amb 1507
1674     markersmoved=true;
1675    
1676     updateURLs();
1677 amb 574 }
1678    
1679    
1680 amb 1033 //
1681 amb 1038 // Close the loop.
1682     //
1683    
1684     function markersLoop() // called from router.html
1685     {
1686     if(vismarkers==mapprops.maxmarkers)
1687     return false;
1688    
1689     if(routino.point[vismarkers].lon==routino.point[1].lon && routino.point[vismarkers].lat==routino.point[1].lat)
1690 amb 1548 {
1691     if(routino.point[vismarkers].active)
1692     return false;
1693     else
1694     {
1695     markerToggleMap(vismarkers);
1696     return true;
1697     }
1698     }
1699 amb 1038
1700     if(routino.point[vismarkers].used)
1701     markerAddForm(++vismarkers);
1702    
1703 amb 1039 markerCopy(vismarkers,1);
1704 amb 1507
1705     markersmoved=true;
1706    
1707     updateURLs();
1708 amb 1548
1709     updateSearchButtons();
1710 amb 1038 }
1711    
1712    
1713     //
1714 amb 1033 // Display the form for a marker
1715     //
1716    
1717     function markerAddForm(marker)
1718     {
1719     document.getElementById("waypoint" + marker).style.display="";
1720     }
1721    
1722    
1723     //
1724     // Hide the form for a marker
1725     //
1726    
1727     function markerRemoveForm(marker)
1728     {
1729     document.getElementById("waypoint" + marker).style.display="none";
1730    
1731     markerClearForm(marker);
1732     }
1733    
1734    
1735     //
1736     // Clear the form for a marker
1737     //
1738    
1739     function markerClearForm(marker)
1740     {
1741     markerRemoveMap(marker);
1742     markerCoords(marker);
1743    
1744     formSetCoords(marker,"","");
1745     formSetSearch(marker,"");
1746    
1747     updateIcon(marker);
1748    
1749     routino.point[marker].used=false;
1750 amb 1036 routino.point[marker].home=false;
1751 amb 1546 routino.point[marker].active=false;
1752 amb 1033 }
1753    
1754    
1755 amb 577 ////////////////////////////////////////////////////////////////////////////////
1756     //////////////////////////// Route results handling ////////////////////////////
1757     ////////////////////////////////////////////////////////////////////////////////
1758    
1759     var route_light_colours={shortest: "#60C060", quickest: "#6060C0"};
1760     var route_dark_colours ={shortest: "#408040", quickest: "#404080"};
1761    
1762     var highlights={shortest: null, quickest: null};
1763     var popups={shortest: null, quickest: null};
1764     var routepoints={shortest: {}, quickest: {}};
1765     var gpx_style={shortest: null, quickest: null};
1766    
1767 amb 569
1768     //
1769 amb 577 // Highlight a specific item in the route
1770 amb 569 //
1771    
1772 amb 1507 function highlight(type,line,action)
1773 amb 569 {
1774 amb 1507 if(action == "clear")
1775 amb 577 {
1776     highlights[type].style.display = "none";
1777 amb 569
1778 amb 1499 drawPopup(type,null);
1779 amb 569 }
1780 amb 1507 else if(action == "zoom")
1781     {
1782     var lonlat = new OpenLayers.LonLat(routepoints[type][line].lon,routepoints[type][line].lat);
1783     lonlat.transform(epsg4326,epsg900913);
1784    
1785 amb 1915 map.moveTo(lonlat,map.numZoomLevels-3);
1786 amb 1507 }
1787 amb 577 else
1788 amb 569 {
1789 amb 577 // Marker
1790 amb 569
1791 amb 1009 var lonlat = new OpenLayers.LonLat(routepoints[type][line].lon,routepoints[type][line].lat);
1792     lonlat.transform(epsg4326,epsg900913);
1793 amb 569
1794 amb 577 highlights[type].move(lonlat);
1795 amb 569
1796 amb 1483 if(highlights[type].style.display == "none")
1797 amb 577 highlights[type].style.display = "";
1798 amb 569
1799 amb 577 // Popup
1800 amb 569
1801 amb 1499 drawPopup(type,"<table>" + routepoints[type][line].html + "</table>");
1802 amb 577 }
1803    
1804     layerVectors.drawFeature(highlights[type]);
1805 amb 569 }
1806    
1807    
1808     //
1809 amb 1491 // Create a popup - independent of map because want it fixed on screen not fixed on map.
1810 amb 569 //
1811    
1812 amb 577 function createPopup(type)
1813 amb 569 {
1814 amb 1483 var popup=document.createElement("div");
1815 amb 569
1816 amb 577 popup.className = "popup";
1817 amb 569
1818 amb 577 popup.innerHTML = "<span></span>";
1819 amb 569
1820 amb 577 popup.style.display = "none";
1821 amb 569
1822 amb 577 popup.style.position = "fixed";
1823     popup.style.top = "-4000px";
1824     popup.style.left = "-4000px";
1825     popup.style.zIndex = "100";
1826 amb 569
1827 amb 577 popup.style.padding = "5px";
1828 amb 569
1829 amb 577 popup.style.opacity=0.85;
1830     popup.style.backgroundColor=route_light_colours[type];
1831     popup.style.border="4px solid " + route_dark_colours[type];
1832 amb 569
1833 amb 577 document.body.appendChild(popup);
1834 amb 569
1835 amb 577 return(popup);
1836 amb 569 }
1837    
1838    
1839     //
1840 amb 1491 // Draw a popup - independent of map because want it fixed on screen not fixed on map.
1841 amb 569 //
1842    
1843 amb 1499 function drawPopup(type,html)
1844 amb 569 {
1845 amb 1499 var popup=popups[type];
1846    
1847 amb 1493 if(html===null)
1848 amb 569 {
1849 amb 577 popup.style.display="none";
1850     return;
1851     }
1852 amb 574
1853 amb 577 if(popup.style.display=="none")
1854 amb 569 {
1855 amb 577 var map_div=document.getElementById("map");
1856 amb 569
1857 amb 577 popup.style.left =map_div.offsetParent.offsetLeft+map_div.offsetLeft+60 + "px";
1858     popup.style.top = map_div.offsetTop +30 + "px";
1859 amb 1499 popup.style.width =map_div.clientWidth-120 + "px";
1860 amb 574
1861 amb 577 popup.style.display="";
1862 amb 569 }
1863    
1864 amb 1507 var close="<span style='float: right; cursor: pointer;' onclick='highlight(\""+type+"\",-1,\"clear\")'>X</span>";
1865 amb 1499
1866     popup.innerHTML=close+html;
1867 amb 569 }
1868    
1869    
1870     //
1871 amb 577 // Remove a GPX trace
1872 amb 569 //
1873    
1874 amb 577 function removeGPXTrace(type)
1875 amb 569 {
1876 amb 577 map.removeLayer(layerGPX[type]);
1877     layerGPX[type].destroy();
1878     layerGPX[type]=null;
1879 amb 569
1880 amb 577 displayStatus(type,"no_info");
1881 amb 569
1882 amb 1011 document.getElementById(type + "_links").style.display = "none";
1883 amb 569
1884 amb 1011 document.getElementById(type + "_route").innerHTML = "";
1885 amb 569
1886 amb 577 hideshow_hide(type);
1887     }
1888 amb 569
1889 amb 574
1890 amb 577 ////////////////////////////////////////////////////////////////////////////////
1891     /////////////////////////////// Server handling ////////////////////////////////
1892     ////////////////////////////////////////////////////////////////////////////////
1893 amb 569
1894 amb 1491 //
1895     // Define an AJAX request object
1896     //
1897 amb 1483
1898 amb 1491 function ajaxGET(url,success,failure,state)
1899     {
1900     var ajaxRequest=new XMLHttpRequest();
1901    
1902     function ajaxGOT(options) {
1903     if(this.readyState==4)
1904     if(this.status==200)
1905     { if(typeof(options.success)=="function") options.success(this,options.state); }
1906     else
1907     { if(typeof(options.failure)=="function") options.failure(this,options.state); }
1908     }
1909    
1910     ajaxRequest.onreadystatechange = function(){ ajaxGOT.call(ajaxRequest,{success: success, failure: failure, state: state}); };
1911     ajaxRequest.open("GET", url, true);
1912     ajaxRequest.send(null);
1913     }
1914    
1915    
1916 amb 569 //
1917     // Display data statistics
1918     //
1919    
1920 amb 1035 function displayStatistics() // called from router.html
1921 amb 569 {
1922     // Use AJAX to get the statistics
1923    
1924 amb 1491 ajaxGET("statistics.cgi", runStatisticsSuccess);
1925 amb 569 }
1926    
1927    
1928     //
1929 amb 577 // Success in running data statistics generation.
1930 amb 569 //
1931    
1932     function runStatisticsSuccess(response)
1933     {
1934 amb 1011 document.getElementById("statistics_data").innerHTML="<pre>" + response.responseText + "</pre>";
1935     document.getElementById("statistics_link").style.display="none";
1936 amb 569 }
1937    
1938    
1939     //
1940 amb 577 // Submit form - perform the routing
1941 amb 569 //
1942    
1943 amb 1035 function findRoute(type) // called from router.html
1944 amb 569 {
1945     tab_select("results");
1946    
1947 amb 1483 hideshow_hide("help_options");
1948     hideshow_hide("shortest");
1949     hideshow_hide("quickest");
1950 amb 569
1951 amb 577 displayStatus("result","running");
1952 amb 569
1953 amb 986 var url="router.cgi" + "?" + buildURLArguments(true) + ";type=" + type;
1954 amb 569
1955     // Destroy the existing layer(s)
1956    
1957 amb 1507 highlight("shortest",-1,"clear");
1958     highlight("quickest",-1,"clear");
1959    
1960 amb 569 if(markersmoved || paramschanged)
1961     {
1962 amb 1493 if(layerGPX.shortest!==null)
1963 amb 569 removeGPXTrace("shortest");
1964 amb 1493 if(layerGPX.quickest!==null)
1965 amb 569 removeGPXTrace("quickest");
1966     markersmoved=false;
1967     paramschanged=false;
1968     }
1969 amb 1493 else if(layerGPX[type]!==null)
1970 amb 569 removeGPXTrace(type);
1971    
1972     // Use AJAX to run the router
1973    
1974     routing_type=type;
1975    
1976 amb 1491 ajaxGET(url, runRouterSuccess, runRouterFailure);
1977 amb 569 }
1978    
1979    
1980     //
1981     // Success in running router.
1982     //
1983    
1984     function runRouterSuccess(response)
1985     {
1986 amb 1483 var lines=response.responseText.split("\n");
1987 amb 569
1988     var uuid=lines[0];
1989 amb 1043 var success=lines[1];
1990 amb 569
1991 amb 629 var link;
1992    
1993 amb 569 // Update the status message
1994    
1995 amb 1043 if(success=="ERROR")
1996 amb 569 {
1997 amb 577 displayStatus("result","error");
1998 amb 1483 hideshow_show("help_route");
1999 amb 629
2000     link=document.getElementById("router_log_error");
2001     link.href="results.cgi?uuid=" + uuid + ";type=router;format=log";
2002    
2003 amb 569 return;
2004     }
2005     else
2006     {
2007 amb 577 displayStatus("result","complete");
2008 amb 1483 hideshow_hide("help_route");
2009 amb 629
2010     link=document.getElementById("router_log_complete");
2011     link.href="results.cgi?uuid=" + uuid + ";type=router;format=log";
2012 amb 569 }
2013    
2014     // Update the routing result message
2015    
2016 amb 577 link=document.getElementById(routing_type + "_html");
2017     link.href="results.cgi?uuid=" + uuid + ";type=" + routing_type + ";format=html";
2018 amb 1011
2019 amb 577 link=document.getElementById(routing_type + "_gpx_track");
2020     link.href="results.cgi?uuid=" + uuid + ";type=" + routing_type + ";format=gpx-track";
2021 amb 1011
2022 amb 577 link=document.getElementById(routing_type + "_gpx_route");
2023     link.href="results.cgi?uuid=" + uuid + ";type=" + routing_type + ";format=gpx-route";
2024 amb 1011
2025 amb 577 link=document.getElementById(routing_type + "_text_all");
2026     link.href="results.cgi?uuid=" + uuid + ";type=" + routing_type + ";format=text-all";
2027 amb 1011
2028 amb 577 link=document.getElementById(routing_type + "_text");
2029     link.href="results.cgi?uuid=" + uuid + ";type=" + routing_type + ";format=text";
2030 amb 569
2031 amb 1491 document.getElementById(routing_type + "_links").style.display = "";
2032 amb 569
2033     // Add a GPX layer
2034    
2035     var url="results.cgi?uuid=" + uuid + ";type=" + routing_type + ";format=gpx-track";
2036    
2037 amb 1013 layerGPX[routing_type] = new OpenLayers.Layer.Vector("GPX (" + routing_type + ")",
2038     {
2039 amb 1491 displayInLayerSwitcher: false,
2040 amb 1013 protocol: new OpenLayers.Protocol.HTTP({url: url, format: new OpenLayers.Format.GPX()}),
2041     strategies: [new OpenLayers.Strategy.Fixed()],
2042     style: gpx_style[routing_type],
2043     projection: map.displayProjection
2044     });
2045 amb 569
2046     map.addLayer(layerGPX[routing_type]);
2047    
2048     hideshow_show(routing_type);
2049    
2050     displayResult(routing_type,uuid);
2051     }
2052    
2053    
2054     //
2055     // Failure in running router.
2056     //
2057    
2058     function runRouterFailure(response)
2059     {
2060 amb 577 displayStatus("result","failed");
2061 amb 569 }
2062    
2063    
2064     //
2065 amb 577 // Display the status
2066     //
2067    
2068     function displayStatus(type,subtype,content)
2069     {
2070 amb 1011 var child=document.getElementById(type + "_status").firstChild;
2071 amb 577
2072     do
2073     {
2074 amb 1493 if(child.id !== undefined)
2075 amb 577 child.style.display="none";
2076    
2077     child=child.nextSibling;
2078     }
2079 amb 1496 while(child !== null);
2080 amb 577
2081 amb 936 var chosen_status=document.getElementById(type + "_status_" + subtype);
2082 amb 577
2083 amb 936 chosen_status.style.display="";
2084 amb 577
2085 amb 1496 if(content !== undefined)
2086 amb 936 chosen_status.innerHTML=content;
2087 amb 577 }
2088    
2089    
2090     //
2091 amb 569 // Display the route
2092     //
2093    
2094     function displayResult(type,uuid)
2095     {
2096     routing_type = type;
2097    
2098     // Add the route
2099    
2100 amb 577 var url="results.cgi?uuid=" + uuid + ";type=" + routing_type + ";format=html";
2101 amb 569
2102     // Use AJAX to get the route
2103    
2104 amb 1491 ajaxGET(url, getRouteSuccess, getRouteFailure);
2105 amb 569 }
2106    
2107    
2108     //
2109     // Success in getting route.
2110     //
2111    
2112     function getRouteSuccess(response)
2113     {
2114 amb 1483 var lines=response.responseText.split("\n");
2115 amb 569
2116 amb 577 routepoints[routing_type]=[];
2117 amb 569
2118 amb 577 var points=routepoints[routing_type];
2119 amb 569
2120 amb 577 var table=0;
2121     var point=0;
2122     var total_table,total_word;
2123 amb 569
2124 amb 577 for(var line=0;line<lines.length;line++)
2125 amb 569 {
2126 amb 577 var thisline=lines[line];
2127 amb 569
2128 amb 1493 if(table===0)
2129 amb 577 {
2130 amb 1483 if(thisline.match("<table>"))
2131 amb 577 table=1;
2132     else
2133     continue;
2134     }
2135 amb 574
2136 amb 1483 if(thisline.match("</table>"))
2137 amb 577 break;
2138    
2139 amb 1483 if(thisline.match("<tr class='([a-z])'>"))
2140 amb 569 {
2141 amb 577 var rowtype=RegExp.$1;
2142 amb 569
2143 amb 1483 if(rowtype=="c")
2144 amb 577 {
2145 amb 1796 thisline.match(": *([-0-9.]+) *([-0-9.]+)");
2146 amb 577 points[point]={lat: Number(RegExp.$1), lon: Number(RegExp.$2), html: "", highway: "", distance: "", total: ""};
2147 amb 574
2148 amb 577 point++;
2149     }
2150 amb 1483 else if(rowtype=="n")
2151 amb 577 {
2152     points[point-1].html += thisline;
2153     }
2154 amb 1483 else if(rowtype=="s")
2155 amb 577 {
2156 amb 1483 thisline.match("<span class='h'>([^<]+)</span>");
2157 amb 577 points[point-1].highway = RegExp.$1;
2158 amb 574
2159 amb 1483 thisline.match("<span class='d'>([^<]+)</span>");
2160 amb 577 points[point-1].distance = RegExp.$1;
2161 amb 574
2162 amb 1483 thisline.match("(<span class='j'>[^<]+</span>)");
2163 amb 577 points[point-1].total = RegExp.$1;
2164    
2165 amb 1483 thisline.match("^(.*).<span class='j'>");
2166 amb 577
2167     points[point-1].html += RegExp.$1;
2168     }
2169 amb 1483 else if(rowtype=="t")
2170 amb 577 {
2171     points[point-1].html += thisline;
2172    
2173 amb 1483 thisline.match("<span class='j'>([^<]+)</span>");
2174 amb 577 points[point-1].total = RegExp.$1;
2175 amb 1796
2176     thisline.match("<td>(.*)");
2177     points[point-1].highway = RegExp.$1;
2178 amb 577 }
2179 amb 569 }
2180     }
2181    
2182 amb 935 displayStatus(routing_type,"info",points[point-1].total.bold());
2183    
2184 amb 1507 var result="<table onmouseout='highlight(\"" + routing_type + "\",-1,\"clear\")'>";
2185 amb 577
2186 amb 1796 for(var p=0;p<point;p++)
2187 amb 577 {
2188 amb 1796 if(p!=point-1)
2189     points[p].html += "<tr><td>" + points[p].total;
2190 amb 577
2191 amb 1800 result=result + "<tr onmouseover='highlight(\"" + routing_type + "\"," + p + ",\"show\")' " +
2192     "onclick='highlight(\"" + routing_type + "\"," + p + ",\"zoom\")'>" +
2193     "<td class='distance' title='" + points[p].distance + "'>#" + (p+1) +
2194 amb 577 "<td class='highway'>" + points[p].highway;
2195     }
2196    
2197 amb 569 result=result + "</table>";
2198    
2199 amb 1011 document.getElementById(routing_type + "_route").innerHTML=result;
2200 amb 569 }
2201    
2202    
2203     //
2204     // Failure in getting route.
2205     //
2206    
2207     function getRouteFailure(response)
2208     {
2209 amb 1011 document.getElementById(routing_type + "_route").innerHTML = "";
2210 amb 569 }
2211 amb 1001
2212    
2213     //
2214     // Perform a search
2215     //
2216    
2217     function DoSearch(marker)
2218     {
2219     // Use AJAX to get the search result
2220    
2221     var search=routino.point[marker].search;
2222    
2223 amb 1491 var mapbounds=map.getExtent().clone();
2224     mapbounds.transform(epsg900913,epsg4326);
2225 amb 1001
2226 amb 1487 var url="search.cgi";
2227 amb 1008
2228 amb 1487 url=url + "?marker=" + marker;
2229     url=url + ";lonmin=" + format5f(mapbounds.left);
2230     url=url + ";latmin=" + format5f(mapbounds.bottom);
2231     url=url + ";lonmax=" + format5f(mapbounds.right);
2232     url=url + ";latmax=" + format5f(mapbounds.top);
2233     url=url + ";search=" + encodeURIComponent(search);
2234    
2235 amb 1493 ajaxGET(url,runSearchSuccess);
2236 amb 1001 }
2237    
2238    
2239 amb 1005 var searchresults=[];
2240    
2241 amb 1001 //
2242     // Success in running search.
2243     //
2244    
2245     function runSearchSuccess(response)
2246     {
2247 amb 1483 var lines=response.responseText.split("\n");
2248 amb 1001
2249     var marker=lines[0];
2250     var cpuinfo=lines[1]; // not used
2251 amb 1483 var message=lines[2];
2252 amb 1001
2253 amb 1493 if(message !== "")
2254 amb 1005 {
2255     alert(message);
2256     return;
2257     }
2258 amb 1001
2259 amb 1005 searchresults[marker]=[];
2260    
2261     for(var line=3;line<lines.length;line++)
2262     {
2263     var thisline=lines[line];
2264    
2265 amb 1493 if(thisline==="")
2266 amb 1005 break;
2267    
2268 amb 1483 thisline.match("([-.0-9]+) ([-.0-9]+) (.*)");
2269 amb 1005
2270     searchresults[marker][searchresults[marker].length]={lat: Number(RegExp.$1), lon: Number(RegExp.$2), name: RegExp.$3};
2271     }
2272    
2273     if(searchresults[marker].length==1)
2274     {
2275     formSetSearch(marker,searchresults[marker][0].name);
2276 amb 1032 formSetCoords(marker,searchresults[marker][0].lon,searchresults[marker][0].lat);
2277     markerAddMap(marker);
2278 amb 1005 }
2279     else
2280     {
2281     var results=document.getElementById("searchresults" + marker);
2282    
2283     var innerHTML="<td colspan=\"3\">";
2284    
2285     for(var n=0;n<searchresults[marker].length;n++)
2286     {
2287     if(n>0)
2288     innerHTML+="<br>";
2289    
2290     innerHTML+="<a href=\"#\" onclick=\"choseSearchResult(" + marker + "," + n + ")\">" +
2291     searchresults[marker][n].name +
2292     "</a>";
2293     }
2294    
2295     results.innerHTML=innerHTML;
2296    
2297     results.style.display="";
2298     }
2299 amb 1001 }
2300 amb 1005
2301    
2302     //
2303     // Display search results.
2304     //
2305    
2306     function choseSearchResult(marker,n)
2307     {
2308     if(n>=0)
2309     {
2310     formSetSearch(marker,searchresults[marker][n].name);
2311 amb 1032 formSetCoords(marker,searchresults[marker][n].lon,searchresults[marker][n].lat);
2312     markerAddMap(marker);
2313 amb 1005 }
2314     }
2315    
2316    
2317     //
2318     // Clear search results.
2319     //
2320    
2321     function clearSearchResult(marker)
2322     {
2323 amb 1011 document.getElementById("searchresults" + marker).style.display="none";
2324 amb 1005 }